public async Task Update(IAccessPermissionCollection accessPermissions)
        {
            // NOTE: this is temporary quick solution
            // Normally it should be replaced with an implementation of partial updates based on MS Sync Framework
            var biometricInfo = await _listUsersBiometricRequest.Request(ListCommand.WithoutParameters);

            var biometricInfoMap            = biometricInfo.Users.ToDictionary(x => x.UserName);
            Func <string, UserHash> getHash = x =>
            {
                var hash = biometricInfoMap.ContainsKey(x) ? biometricInfoMap[x].BiometricHash : null;
                return(new UserHash(x, hash));
            };

            // add user permissions
            var accessRights = await _listAccessRightsRequest.Request(ListCommand.WithoutParameters);

            foreach (var userAccessRights in accessRights.UserAccessRights)
            {
                var userName = userAccessRights.UserName;
                var userHash = getHash(userName);

                userAccessRights.PermanentAccessRules.ForEach(
                    rule =>
                {
                    var permission = new PermanentUserAccess(rule.AccessPointId, userHash);
                    accessPermissions.AddOrUpdatePermission(permission);
                });

                userAccessRights.ScheduledAccessRules.ForEach(
                    rule =>
                {
                    var permission = new ScheduledUserAccess(rule.AccessPointId, userHash, WeeklySchedule.Convert(rule.WeeklySchedule));
                    accessPermissions.AddOrUpdatePermission(permission);
                });
            }

            // add user group permissions
            foreach (var groupRights in accessRights.UserGroupAccessRights)
            {
                var groupName          = groupRights.UserGroupName;
                var usersInGroupResult = await _listUsersInGroupRequest.Request(ListCommand.ListUsersInGroup(groupName));

                var userHashes = usersInGroupResult.Users.Select(x => getHash(x.UserName)).ToArray();

                groupRights.PermanentAccessRules.ForEach(
                    rule =>
                {
                    var permission = new PermanentGroupAccess(rule.AccessPointId, groupName, userHashes);
                    accessPermissions.AddOrUpdatePermission(permission);
                });

                groupRights.ScheduledAccessRules.ForEach(
                    rule =>
                {
                    var permission = new ScheduledGroupAccess(rule.AccessPointId, groupName, userHashes, WeeklySchedule.Convert(rule.WeeklySchedule));
                    accessPermissions.AddOrUpdatePermission(permission);
                });
            }
        }
Example #2
0
        public Task Consume(ConsumeContext <IScheduledGroupAccessAllowed> context)
        {
            var hashes     = CombineHashes(context.Message.UsersInGroup, context.Message.UsersBiometrics);
            var permission = new ScheduledGroupAccess(context.Message.AccessPointId, context.Message.UserGroupName, hashes, WeeklySchedule.Convert(context.Message.WeeklySchedule));

            _accessPermissions.AddOrUpdatePermission(permission);
            _service.Save(_accessPermissions);
            return(Task.FromResult(true));
        }
Example #3
0
        public void Visit(ScheduledGroupAccess permission)
        {
            var hashes = permission.UserHashes.ToList();
            var index  = hashes.FindIndex(x => string.Equals(x.UserName, _userName));

            if (index == -1)
            {
                return;
            }

            var old = hashes[index];

            hashes[index] = new UserHash(old.UserName, _newHash);
            var replacement = new ScheduledGroupAccess(permission.AccessPointId, permission.UserGroupName, hashes.ToArray(), permission.WeeklySchedule);

            _accessPermissions.AddOrUpdatePermission(replacement);
        }