Exemplo n.º 1
0
        public static IViewModel SaveRoleRights(Guid roleId, List <Guid> ids)
        {
            try
            {
                using (DBContext context = DBContext.Create())
                {
                    ResultViewModel result = new ResultViewModel
                    {
                        Result = Result.Ok
                    };

                    context.Configuration.AutoDetectChangesEnabled = false;
                    context.Configuration.ValidateOnSaveEnabled    = false;

                    var roleRights = context.RightRoles.Where(o => o.RoleId == roleId).AsNoTracking();
                    foreach (var role in roleRights.Where(o => !ids.Contains(o.RightId)))
                    {
                        context.Entry(role).State = EntityState.Deleted;
                    }
                    foreach (var id in ids.Where(o => !roleRights.Select(r => r.RightId).Contains(o)))
                    {
                        RightRole rightRole = new RightRole {
                            RightId = id, RoleId = roleId
                        };
                        context.Entry(rightRole).State = EntityState.Added;
                    }
                    context.SaveChanges();

                    context.Configuration.AutoDetectChangesEnabled = true;
                    context.Configuration.ValidateOnSaveEnabled    = true;

                    return(result);
                }
            }
            catch (Exception exc)
            {
                return(LogErrorManager.Add(exc));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 保存权限角色
        /// </summary>
        /// <param name="badge"></param>
        /// <param name="item"></param>
        public void SaveRole(KeyValuePair <string, string> user, string applicationId, RightRole item)
        {
            if (applicationId == ConfigurationManager.AppSettings["RightApplicationId"])
            {
                throw new InfoException("不能新增/编辑权限管理应用的权限角色");
            }
            if (!string.IsNullOrEmpty(item.OuterUrl))
            {
                try
                {
                    Util.HttpGet <List <string> >(item.OuterUrl);
                }
                catch
                {
                    throw new InfoException("外部接口无效");
                }
            }

            db.BeginTransaction();
            try
            {
                if (db.Exists <RightRole>("ApplicationId = @0 AND RoleName = @1 and Id != @2", applicationId, item.RoleName, item.Id))
                {
                    throw new InfoException("角色名称【{0}】已存在", item.RoleName);
                }
                if (db.IsNew(item))
                {
                    db.Insert(item);
                    logService.WriteInsertOperateLog(user, applicationId, item.Id, item);
                }
                else
                {
                    var oldItem = db.SingleOrDefault <RightRole>(item.Id);
                    if (oldItem == null)
                    {
                        throw new InfoException("【{0}】记录不存在", item.Id);
                    }
                    db.Update(item);
                    logService.WriteUpdateOperateLog(user, applicationId, item.Id, oldItem, item);
                }
                db.CompleteTransaction();
            }
            catch (Exception e)
            {
                db.AbortTransaction();
                throw e;
            }
        }