public SystemRole UpdateSystemRole(string systemRoleId, string roleName, EnumSystemRoleType roleType, SystemRolePermissions permissions, SystemRoleAssignments assignments) { if(string.IsNullOrEmpty(roleName)) { throw new ArgumentNullException("roleName"); } using(var db = _sqlConnectionInfo.GetDB()) { var count = db.ExecuteScalar<int>("SELECT COUNT(*) FROM SystemRole WHERE RoleName=@0 AND ID<>@1", roleName, systemRoleId); if(count != 0) { throw new ArgumentException("RoleName already exists: " + roleName); } } var item = this.GetDBItem(systemRoleId); item.RoleName = roleName; item.RoleType = roleType; item.PermissionsJson = (permissions ?? new SystemRolePermissions()).ToJson(); item.SetUpdatedFields(_userIdentity.UserName); using(var db = _sqlConnectionInfo.GetDB()) { db.Update("SystemRole", "ID", item, item.Id); if(assignments == null || assignments.UserNameList == null || assignments.UserNameList.Count == 0) { db.Execute("DELETE FROM SystemRoleUser WHERE SystemRoleID=@0", systemRoleId); } else { var itemsToAdd = new List<string>(); var itemsToDelete = new List<SqlSystemRoleUser>(); foreach(var newUserName in assignments.UserNameList) { if(!item.SystemRoleUserList.Any(i=>i.UserName.Equals(newUserName, StringComparison.CurrentCultureIgnoreCase))) { itemsToAdd.Add(newUserName); } } foreach(var oldUser in item.SystemRoleUserList) { if(!assignments.UserNameList.Contains(oldUser.UserName, StringComparer.CurrentCultureIgnoreCase)) { itemsToDelete.Add(oldUser); } } foreach(var newUserName in itemsToAdd) { var dbItem = new SqlSystemRoleUser { Id = Guid.NewGuid().ToString(), SystemRoleId = systemRoleId, UserName = newUserName }; db.Insert("SystemRoleUser", "ID", false, dbItem); } foreach(var oldUser in itemsToDelete) { db.Execute("DELETE FROM SystemRoleUser WHERE ID=@0", oldUser.Id); } } } return this.GetSystemRole(item.Id); }
public SystemRole CreateSystemRole(string roleName, EnumSystemRoleType roleType, SystemRolePermissions permissions, SystemRoleAssignments assignments) { if(string.IsNullOrEmpty(roleName)) { throw new ArgumentNullException("roleName"); } using (var db = _sqlConnectionInfo.GetDB()) { var count = db.ExecuteScalar<int>("SELECT COUNT(*) FROM SystemRole WHERE RoleName=@0", roleName); if (count != 0) { throw new ArgumentException("RoleName already exists: " + roleName); } } var item = new SqlSystemRole { Id = Guid.NewGuid().ToString(), RoleName = roleName, RoleType = roleType, PermissionsJson = (permissions ?? new SystemRolePermissions()).ToJson(), //AssignmentsJson = (assignments ?? new SystemRoleAssignments()).ToJson() }; item.SetCreatedFields(_userIdentity.UserName); using(var db = _sqlConnectionInfo.GetDB()) { db.Insert("SystemRole", "ID", false, item); if(assignments != null && assignments.UserNameList != null) { foreach(var userName in assignments.UserNameList) { var userItem = new SqlSystemRoleUser { Id = Guid.NewGuid().ToString(), SystemRoleId = item.Id, UserName = userName }; db.Insert("SystemRoleUser", "ID", false, userItem); } } } return this.GetSystemRole(item.Id); }