protected void RemovePermissionsFromRole(List<Permission> permissions, Role role, Func<ICollection<Permission>, Permission, ICollection<Permission>> changePermission) { if (permissions == null || permissions.Count == 0 || role == null) return; StringBuilder message = new StringBuilder(), items = new StringBuilder(); ; foreach (var perm in permissions) { if (perm != null) { if (role.Permissions.Contains<Permission>(perm)) { changePermission(role.Permissions, perm); items.AppendFormat("{0},", perm.Name); } } } using (var scope = new UnitOfWorkScope()) { _roleRepository.Update(role); scope.Commit(); } if (items.Length > 0) { items = items.Remove(items.Length - 1, 1); message.AppendFormat("Permission(s) {0} are removed from role {1}.", items.ToString(), role.Name); ActivityLog item = new ActivityLog(ActivityType.RemovePermissionFromRole.ToString(), message.ToString()); _activityLogService.Add(item); } }
public virtual bool UpdateRole(Role newRole) { var role = _roleRepository.Query.Where(x => x.Id == newRole.Id).SingleOrDefault(); role.Name = newRole.Name; role.Description = newRole.Description; using (var scope = new UnitOfWorkScope()) { _roleRepository.Update(role); scope.Commit(); } StringBuilder message=new StringBuilder(); message.AppendFormat("Role {0} is updated.",role.Name); ActivityLog item=new ActivityLog(ActivityType.UpdateRole.ToString(),message.ToString()); _activityLogService.Add(item); return true; }
private static void SeedDatabase() { string userEmail = ConfigurationManager.AppSettings[Constants.ADMIN_EMAIL]; if (!string.IsNullOrWhiteSpace(userEmail)) { IUserService userService = IoC.GetService<IUserService>(); //string username = userEmail.Substring(0, userEmail.IndexOf('@') > 0 ? userEmail.IndexOf('@') : userEmail.Length); string username = userEmail; var account = userService.FindBy(x => x.Username == username || x.Email==userEmail); if (account == null) { NhUserAccount user = new NhUserAccount() { Username = username, Email = userEmail, HashedPassword = "******", FirstName = "Admin",IsLoginAllowed=true,IsAccountVerified=true}; account = userService.CreateAccountWithTempPassword(user); } IRoleService roleServie = IoC.GetService<IRoleService>(); Role adminRole = new Role { Name = Constants.ROLE_ADMIN, Description = "System Administrator" }; if (!roleServie.RoleExists(adminRole.Name)) { roleServie.CreateRole(adminRole); } if (!roleServie.IsUserInRole(account.ID, Constants.ROLE_ADMIN)) roleServie.AddUsersToRoles(new List<Guid>() { account.ID }, new List<string>() { adminRole.Name }); } }
public virtual void CreateRole(Role role) { if (role == null) throw new ArgumentNullException("Role"); using (var scope = new UnitOfWorkScope()) { _roleRepository.Add(role); scope.Commit(); } StringBuilder message=new StringBuilder(); message.AppendFormat("Role {0} is created.",role.Name); ActivityLog item=new ActivityLog(ActivityType.AddRole.ToString(),message.ToString()); _activityLogService.Add(item); }