예제 #1
0
        public ApiResult EditRoleAuths([FromBody] RequestEditRoleAuthDTO request)
        {
            string reason = "";

            if (userService.EditRoleAuths(request, out reason))
            {
                return(new ApiResult());
            }
            else
            {
                return(new ApiResult()
                {
                    Status = EnumApiStatus.BizError, Msg = reason
                });
            }
        }
예제 #2
0
        public bool EditRoleAuths(RequestEditRoleAuthDTO dto, out string reason)
        {
            reason = "";
            using (var db = new DBEntities())
            {
                var model = db.Roles.Where(t => t.ID == dto.ID).FirstOrDefault();

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

                    var olds = (from auths in db.AuthModels.Where(t => t.Type == dto.Type)
                                join rolemap in db.AuthRoleMaps on auths.AuthID equals rolemap.AuthID
                                where rolemap.RoleID == dto.ID
                                select rolemap).ToList();
                    foreach (var item in olds)
                    {
                        db.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                    }

                    var authlist = dto.AuthLists.Where(t => t.Selected).Select(t => t.AuthID).ToList();

                    var listexit = db.AuthModels.Where(t => authlist.Contains(t.AuthID)).Select(t => t.AuthID).ToList();

                    foreach (var item in listexit)
                    {
                        AuthRoleMap authRoleMap = new AuthRoleMap()
                        {
                            MapID  = Guid.NewGuid().ToString("N"),
                            RoleID = dto.ID,
                            AuthID = item
                        };
                        db.AuthRoleMaps.Add(authRoleMap);
                    }
                }
                return(db.SaveChanges() > 0);
            }
        }