Example #1
0
        public ApiResult EditUserRoles([FromBody] RequestEditUserRoleDTO request)
        {
            string reason = "";

            if (userService.EditUserRoles(request, out reason))
            {
                return(new ApiResult());
            }
            else
            {
                return(new ApiResult()
                {
                    Status = EnumApiStatus.BizError, Msg = reason
                });
            }
        }
Example #2
0
        public bool EditUserRoles(RequestEditUserRoleDTO dto, out string reason)
        {
            reason = "";
            using (var db = new DBEntities())
            {
                var model = db.Users.Where(t => t.ID == dto.ID && !t.IsDeleted).FirstOrDefault();

                if (model == null)
                {
                    reason = "账号不存在";
                    return(false);
                }
                else
                {
                    if (model.IsSystem)
                    {
                        reason = "系统账号不允许修改";
                        return(false);
                    }

                    var olds = db.UserRoleMaps.Where(t => t.UserID == dto.ID).ToList();
                    foreach (var item in olds)
                    {
                        db.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                    }

                    var rolelist = dto.RoleLists.Where(t => t.Selected).Select(t => t.ID).ToList();

                    var listexit = db.Roles.Where(t => rolelist.Contains(t.ID) && t.State == 0).Select(t => t.ID).ToList();

                    foreach (var item in listexit)
                    {
                        UserRoleMap userRoleMap = new UserRoleMap()
                        {
                            MapID  = Guid.NewGuid().ToString("N"),
                            RoleID = item,
                            UserID = dto.ID
                        };
                        db.UserRoleMaps.Add(userRoleMap);
                    }
                }
                return(db.SaveChanges() > 0);
            }
        }