public void AddTest() { var model = new SysUserRole { SysId = "cf9d52cc-0500-4829-9611-fd0056961477", UserId = "cf9d52cc-0500-4829-9611-fd0056961468", RoleId = "cf9d52cc-0500-4829-9611-fd0056961488" }; Console.WriteLine(repository.Add(model)); model = new SysUserRole { SysId = "cf9d52cc-0500-4829-9611-fd0056961478", UserId = "cf9d52cc-0500-4829-9611-fd0056961469", RoleId = "cf9d52cc-0500-4829-9611-fd0056961489" }; Console.WriteLine(repository.Add(model)); }
//修改用户的角色 public JsonResult UpdateRolesForUser(string roleId, string userIds) { string[] userIDs = userIds.Split(','); List<SysUserRole> userRoles = new List<SysUserRole>(); for (int i = 0; i < userIDs.Length; i++) { if (!string.IsNullOrEmpty(userIDs[i])) { SysUserRole userRole = new SysUserRole() { SysId = Util.NewId(), UserId = userIDs[i], RoleId = roleId }; userRoles.Add(userRole); } } if (userRoles.Count == 0) { userRoles.Add(new SysUserRole() { SysId = Util.NewId(), UserId = null, RoleId = roleId }); } /* Logger.LogMsg( LogLevel.Info, string.Format("修改用户的角色,roleId:{0},userIds:{1} 成功", roleId, userIds), UserData.SysId, UserData.UserName, "RestApi->UpdateRolesForUser");*/ return Json(roleService.SetUserRole(userRoles), JsonRequestBehavior.AllowGet); }
//保存用户角色 public JsonResult SetUserRoles(string userId, string roleIds) { string[] ids = roleIds.Split(','); using (var tran = RepositoryFactory.ConfigRepository.Connection.BeginTransaction()) { RepositoryFactory.UserRoleRepository.DeleteByUserId(userId, tran); for (int i = 0; i < ids.Length; i++) { if (!string.IsNullOrEmpty(ids[i])) { SysUserRole userRole = new SysUserRole() { UserId = userId, RoleId = ids[i] }; if (RepositoryFactory.UserRoleRepository.Add(userRole, tran) == 0) { tran.Rollback(); return Json(false); } } } tran.Commit(); } return Json(true); }
public bool AddUserRole(SysUserRole userRole) { return this.UserRoleRepository.Add(userRole) != 0; }