forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPFRole.m
91 lines (70 loc) · 2.65 KB
/
PFRole.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "PFRole.h"
#import <Bolts/BFTask.h>
#import "PFAssert.h"
#import "PFObject+Subclass.h"
#import "PFObjectPrivate.h"
#import "PFQuery.h"
@implementation PFRole
///--------------------------------------
#pragma mark - Init
///--------------------------------------
- (instancetype)initWithName:(NSString *)name {
return [self initWithName:name acl:nil];
}
- (instancetype)initWithName:(NSString *)name acl:(PFACL *)acl {
self = [super init];
if (!self) return nil;
self.name = name;
self.ACL = acl;
return self;
}
+ (instancetype)roleWithName:(NSString *)name {
return [[self alloc] initWithName:name];
}
+ (instancetype)roleWithName:(NSString *)name acl:(PFACL *)acl {
return [[self alloc] initWithName:name acl:acl];
}
///--------------------------------------
#pragma mark - Role-specific Properties
///--------------------------------------
@dynamic name;
// Dynamic synthesizers would use objectForKey, not relationForKey
- (PFRelation<PFRole *> *)roles {
return [self relationForKey:@keypath(PFRole, roles)];
}
- (PFRelation<PFUser *> *)users {
return [self relationForKey:@keypath(PFRole, users)];
}
///--------------------------------------
#pragma mark - PFObject Overrides
///--------------------------------------
- (void)setObject:(id)object forKey:(NSString *)key {
if ([key isEqualToString:@keypath(PFRole, name)]) {
PFConsistencyAssert(!self.objectId, @"A role's name can only be set before it has been saved.");
PFParameterAssert([object isKindOfClass:[NSString class]], @"A role's name must be an NSString.");
PFParameterAssert([object rangeOfString:@"^[0-9a-zA-Z_\\- ]+$" options:NSRegularExpressionSearch].location != NSNotFound,
@"A role's name can only contain alphanumeric characters, _, -, and spaces.");
}
[super setObject:object forKey:key];
}
- (BFTask *)saveInBackground {
PFConsistencyAssert(self.objectId || self.name, @"New roles must specify a name.");
return [super saveInBackground];
}
// Validates a class name. We override this to only allow the role class name.
+ (void)_assertValidInstanceClassName:(NSString *)className {
PFParameterAssert([className isEqualToString:[self parseClassName]],
@"Cannot initialize a PFRole with a custom class name.");
}
+ (NSString *)parseClassName {
return @"_Role";
}
@end