Exemple #1
0
        /// <summary>
        /// Authorize permission
        /// </summary>
        /// <param name="permissionRecordSystemName">Permission record system name</param>
        /// <param name="customerRole">Customer role</param>
        /// <returns>true - authorized; otherwise, false</returns>
        protected virtual bool Authorize(string permissionRecordSystemName, UserRole userRole) {
            if (String.IsNullOrEmpty(permissionRecordSystemName))
                return false;

            string key = string.Format(PERMISSIONS_ALLOWED_KEY, userRole.Id, permissionRecordSystemName);
            return _cacheManager.Get(key, ctx => {
                ctx.Monitor(_signals.When(PERMISSIONS_PATTERN_KEY));

                foreach (var permission1 in userRole.PermissionRecords)
                    if (permission1.SystemName.Equals(permissionRecordSystemName, StringComparison.InvariantCultureIgnoreCase))
                        return true;

                return false;
            });
        }
 protected UserRoleModel PrepareUserRoleModel(UserRole userRole) {
     var model = userRole.ToModel();
     return model;
 }
        protected virtual void InstallUsersAndRoles(string defaultUsername, string defaultUserPassword) {
            var crAdministrators = new UserRole {
                Name = "Administrators",
                Active = true,
                IsSystemRole = true,
                SystemName = SystemUserRoleNames.Administrators,
            };
            _userRoleRepository.Insert(crAdministrators);

            var crRegistered = new UserRole {
                Name = "Registered",
                Active = true,
                IsSystemRole = true,
                SystemName = SystemUserRoleNames.Registered,
            };
            _userRoleRepository.Insert(crRegistered);

            var crGuests = new UserRole {
                Name = "Guests",
                Active = true,
                IsSystemRole = true,
                SystemName = SystemUserRoleNames.Guests,
            };
            _userRoleRepository.Insert(crGuests);

            //admin user
            var adminUser = new User {
                UserGuid = Guid.NewGuid(),
                Email = "",
                Username = defaultUsername,
                Password = defaultUserPassword,
                PasswordFormat = PasswordFormat.Clear,
                PasswordSalt = "",
                Active = true,
                DepartmentId = 1,
                CreatedOnUtc = DateTime.UtcNow,
                LastActivityDateUtc = DateTime.UtcNow,
            };

            adminUser.UserRoles.Add(crAdministrators);
            adminUser.UserRoles.Add(crRegistered);
            _userRepository.Insert(adminUser);

        }
Exemple #4
0
        /// <summary>
        /// Install permissions
        /// </summary>
        /// <param name="permissionProvider">Permission provider</param>
        public virtual void InstallPermissions(IPermissionProvider permissionProvider) {
            //install new permissions
            var permissions = permissionProvider.GetPermissions();
            foreach (var permission in permissions) {
                var permission1 = GetPermissionRecordBySystemName(permission.SystemName);
                if (permission1 == null) {
                    //new permission (install it)
                    permission1 = new PermissionRecord {
                        Name = permission.Name,
                        SystemName = permission.SystemName,
                        Category = permission.Category,
                    };


                    //default customer role mappings
                    var defaultPermissions = permissionProvider.GetDefaultPermissions();
                    foreach (var defaultPermission in defaultPermissions) {
                        var userRole = _userService.GetUserRoleBySystemName(defaultPermission.CustomerRoleSystemName);
                        if (userRole == null) {
                            //new role (save it)
                            userRole = new UserRole {
                                Name = defaultPermission.CustomerRoleSystemName,
                                Active = true,
                                SystemName = defaultPermission.CustomerRoleSystemName
                            };
                            _userService.InsertUserRole(userRole);
                        }


                        var defaultMappingProvided = (from p in defaultPermission.PermissionRecords
                                                      where p.SystemName == permission1.SystemName
                                                      select p).Any();
                        var mappingExists = (from p in userRole.PermissionRecords
                                             where p.SystemName == permission1.SystemName
                                             select p).Any();
                        if (defaultMappingProvided && !mappingExists) {
                            permission1.UserRoles.Add(userRole);
                        }
                    }

                    //save new permission
                    InsertPermissionRecord(permission1);

                    //save localization
                    permission1.SaveLocalizedPermissionName(_localizationService, _languageService);
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Updates the User role
        /// </summary>
        /// <param name="UserRole">User role</param>
        public virtual void UpdateUserRole(UserRole UserRole) {
            if (UserRole == null)
                throw new ArgumentNullException("UserRole");

            _userRoleRepository.Update(UserRole);

            _signals.Trigger(USERROLES_PATTERN_KEY);
        }
Exemple #6
0
        /// <summary>
        /// Delete a User role
        /// </summary>
        /// <param name="UserRole">User role</param>
        public virtual void DeleteUserRole(UserRole UserRole) {
            if (UserRole == null)
                throw new ArgumentNullException("UserRole");

            if (UserRole.IsSystemRole)
                throw new NutException("System role could not be deleted");

            _userRoleRepository.Delete(UserRole);

            _signals.Trigger(USERROLES_PATTERN_KEY);

        }