public override void AddUsersToRoles(string[] usernames, string[] roleNames) { if (_App == null) { return; } IuserService UserSrv = new userService(SessionFactoryConfigPath); IroleService RoleSrv = new roleService(SessionFactoryConfigPath); foreach (string UN in usernames) { user mUser = UserSrv.GetByName(UN, _App.AppID); if (mUser != null) { string[] currentRoles = (from r in mUser.Roles where r.AppID == _App.AppID select r.name).ToArray(); foreach (string r in roleNames) { if (!currentRoles.Contains(r)) { role mRole = RoleSrv.GetByName(r, _App.AppID); if (mRole != null) { mUser.Roles.Add(mRole); } } } UserSrv.Save(mUser); } } UserSrv.CommitChanges(); }
private void updateRolesForUser(user mUser, string[] roleNames) { IuserService UserSrv = new userService(SessionFactoryConfigPath); IroleService RoleSrv = new roleService(SessionFactoryConfigPath); if (mUser != null) { if (roleNames == null || roleNames.Length <= 0) { if (mUser.Roles != null) { mUser.Roles.Clear(); } } else { if (mUser.Roles == null) { mUser.Roles = new List <role>(); } string[] currentRoles = (from r in mUser.Roles where r.AppID == _App.AppID select r.name).ToArray(); string[] RemoveRoles = (from rl in currentRoles where !roleNames.Contains(rl) select rl).ToArray(); string[] InsertRoles = (from rl in roleNames where !currentRoles.Contains(rl) select rl).ToArray(); //remove role foreach (string r in RemoveRoles) { role mRole = RoleSrv.GetByName(r, _App.AppID); if (mRole != null) { mUser.Roles.Remove(mRole); } } foreach (string r in InsertRoles) { role mRole = RoleSrv.GetByName(r, _App.AppID); if (mRole != null) { mUser.Roles.Add(mRole); } } } UserSrv.Save(mUser); UserSrv.CommitChanges(); } }