1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2017 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #import "RLMSyncPermission_Private.hpp"
20 #import "RLMSyncUtil_Private.hpp"
24 using namespace realm;
25 using ConditionType = Permission::Condition::Type;
27 #pragma mark - Permission
29 @interface RLMSyncPermission () {
34 util::Optional<Permission> _underlying;
35 RLMSyncAccessLevel _accessLevel;
41 @implementation RLMSyncPermission
43 - (instancetype)initWithRealmPath:(NSString *)path
44 identity:(NSString *)identity
45 accessLevel:(RLMSyncAccessLevel)accessLevel {
46 if (self = [super init]) {
47 _accessLevel = accessLevel;
51 @throw RLMException(@"A permission value cannot be created without a valid user ID");
53 _updatedAt = [NSDate date];
58 - (instancetype)initWithRealmPath:(NSString *)path
59 username:(NSString *)username
60 accessLevel:(RLMSyncAccessLevel)accessLevel {
61 if (self = [super init]) {
62 _accessLevel = accessLevel;
67 _updatedAt = [NSDate date];
72 - (instancetype)initWithPermission:(Permission)permission {
73 if (self = [super init]) {
74 _underlying = util::make_optional<Permission>(std::move(permission));
85 return @(_underlying->path.c_str());
88 - (RLMSyncAccessLevel)accessLevel {
92 return objCAccessLevelForAccessLevel(_underlying->access);
96 return self.accessLevel > RLMSyncAccessLevelNone;
100 return self.accessLevel > RLMSyncAccessLevelRead;
104 return self.accessLevel == RLMSyncAccessLevelAdmin;
107 - (NSString *)identity {
111 if (_underlying->condition.type == ConditionType::UserId) {
112 return @(_underlying->condition.user_id.c_str());
121 if (_underlying->condition.type == ConditionType::KeyValue) {
122 return @(_underlying->condition.key_value.first.c_str());
127 - (NSString *)value {
131 if (_underlying->condition.type == ConditionType::KeyValue) {
132 return @(_underlying->condition.key_value.second.c_str());
137 - (NSDate *)updatedAt {
141 return RLMTimestampToNSDate(_underlying->updated_at);
144 - (realm::Permission)rawPermission {
148 auto condition = (_identity
149 ? Permission::Condition([_identity UTF8String])
150 : Permission::Condition([_key UTF8String], [_value UTF8String]));
153 accessLevelForObjCAccessLevel(_accessLevel),
159 return [self.identity hash] ^ self.accessLevel;
162 - (BOOL)isEqual:(id)object {
163 if (self == object) {
166 if ([object isKindOfClass:[RLMSyncPermission class]]) {
167 RLMSyncPermission *that = (RLMSyncPermission *)object;
168 return (self.accessLevel == that.accessLevel
169 && Permission::paths_are_equivalent([self.path UTF8String], [that.path UTF8String],
170 [self.identity UTF8String], [that.identity UTF8String])
171 && [self.identity isEqualToString:that.identity]);
176 - (NSString *)description {
177 NSString *typeDescription = nil;
179 typeDescription = [NSString stringWithFormat:@"identity: %@", self.identity];
181 typeDescription = [NSString stringWithFormat:@"key: %@, value: %@", self.key, self.value];
183 return [NSString stringWithFormat:@"<RLMSyncPermission> %@, path: %@, access level: %@",
186 @(Permission::description_for_access_level(accessLevelForObjCAccessLevel(self.accessLevel)).c_str())];