Example #1
0
 public IActionResult AssignRolesToUser([FromBody] AddUserRolesModel model)
 {
     foreach (var rid in model.RoleId)
     {
         var role = _roleService.FindById(rid);
         if (role.Name.IsCaseInsensitiveEqual("administrator"))
         {
             return(JError(T["notallow_edit"]));
         }
     }
     if (model.UserId.IsEmpty())
     {
         return(NotSpecifiedRecord());
     }
     foreach (var uid in model.UserId)
     {
         var userRoles = new List <SystemUserRoles>();
         foreach (var rid in model.RoleId)
         {
             var entity = new SystemUserRoles
             {
                 SystemUserId     = uid,
                 RoleId           = rid,
                 SystemUserRoleId = Guid.NewGuid()
             };
             userRoles.Add(entity);
         }
         _systemUserRolesService.UpdateUserRoles(uid, userRoles);
     }
     return(SaveSuccess());
 }
Example #2
0
        public IActionResult AddUserRoles([FromBody] AddUserRolesModel model)
        {
            if (model.UserId.IsEmpty() || model.RoleId.IsEmpty())
            {
                return(NotSpecifiedRecord());
            }
            var userRoles       = new List <SystemUserRoles>();
            var existsUserRoles = _systemUserRolesService.Query(x => x.Where(f => f.SystemUserId.In(model.UserId) && f.RoleId.In(model.RoleId)));

            foreach (var uid in model.UserId)
            {
                foreach (var rid in model.RoleId)
                {
                    if (existsUserRoles.Exists(x => x.RoleId == rid && x.SystemUserId == uid))
                    {
                        continue;
                    }
                    var entity = new SystemUserRoles
                    {
                        SystemUserId     = uid,
                        RoleId           = rid,
                        SystemUserRoleId = Guid.NewGuid()
                    };
                    userRoles.Add(entity);
                }
            }
            if (userRoles.NotEmpty())
            {
                _systemUserRolesService.CreateMany(userRoles);
            }
            return(SaveSuccess());
        }