Beispiel #1
0
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task <ResultDto <Role> > CreateAsync(Role entity, List <long> permissionIds, CurrentUser currentUser)
        {
            entity.CreatorTime = DateTime.Now;
            var moduledata = await ModuleApp.GetSaasModuleListAsync();

            var buttondata = await ModuleButtonApp.GetSaasModuleListAsync();

            List <long> allpermissionIds = new List <long>();

            foreach (long id in permissionIds)
            {
                allpermissionIds.Add(id);
                if (moduledata.Count(o => o.Id == id) > 0)
                {
                    var md = moduledata.Where(o => o.Id == id).FirstOrDefault();
                    if (md.ParentId != 0)
                    {
                        allpermissionIds.AddRange(await Fibonacci(md.ParentId));
                    }
                }
                else if (buttondata.Count(o => o.Id == id) > 0)
                {
                    var md = buttondata.Where(o => o.Id == id).FirstOrDefault();
                    if (md.ModuleId != 0)
                    {
                        allpermissionIds.AddRange(await Fibonacci(md.ModuleId));
                    }
                }
            }
            allpermissionIds = allpermissionIds.Distinct().ToList();
            List <RoleAuthorize> ras = new List <RoleAuthorize>();

            foreach (long id in allpermissionIds)// permissionIds
            {
                int itemType = 0;
                if (moduledata.Count(o => o.Id == id) > 0)
                {
                    itemType = 1;
                }
                else if (buttondata.Count(o => o.Id == id) > 0)
                {
                    itemType = 2;
                }
                if (itemType > 0)
                {
                    RoleAuthorize ra = new RoleAuthorize
                    {
                        ObjectId    = entity.Id,
                        ObjectType  = 1,
                        ItemId      = id,
                        ItemType    = itemType,
                        CreatorTime = DateTime.Now
                    };

                    ras.Add(ra);
                }
            }

            UnitWork.Add <Role>(entity);
            UnitWork.BatchAdd <RoleAuthorize>(ras.ToArray());
            UnitWork.Save();

            if (currentUser != null)
            {
                await OperateLogApp.InsertLogAsync <Role>(currentUser, "添加角色", entity);
            }
            return(ResultDto <Role> .Suc(entity));
        }
Beispiel #2
0
        public async Task <ResultDto <Role> > UpdateAsync(Role entity, List <long> permissionIds, CurrentUser currentUser)
        {
            var moduledata = await ModuleApp.GetSaasModuleListAsync();

            var buttondata = await ModuleButtonApp.GetSaasModuleListAsync();

            var authorizs = await RoleAuthorizeApp.GetListAsync(entity.Id, 1);

            List <long> allpermissionIds = new List <long>();

            foreach (long id in permissionIds)
            {
                allpermissionIds.Add(id);
                if (moduledata.Count(o => o.Id == id) > 0)
                {
                    var md = moduledata.Where(o => o.Id == id).FirstOrDefault();
                    if (md.ParentId != 0)
                    {
                        allpermissionIds.AddRange(await Fibonacci(md.ParentId));
                    }
                }
                else if (buttondata.Count(o => o.Id == id) > 0)
                {
                    var md = buttondata.Where(o => o.Id == id).FirstOrDefault();
                    if (md.ModuleId != 0)
                    {
                        allpermissionIds.AddRange(await Fibonacci(md.ModuleId));
                    }
                }
            }
            allpermissionIds = allpermissionIds.Distinct().ToList();
            //现有
            List <long>          itemIds   = authorizs.Select(o => o.ItemId).ToList();
            List <long>          deleteIds = authorizs.Where(o => !allpermissionIds.Contains(o.ItemId) && o.ObjectId == entity.Id && o.ObjectType == 1).Select(o => o.Id).ToList();
            List <RoleAuthorize> ras       = new List <RoleAuthorize>();

            foreach (long id in allpermissionIds)
            {
                if (itemIds.Contains(id))
                {
                    continue;
                }
                int itemType = 0;
                if (moduledata.Count(o => o.Id == id) > 0)
                {
                    itemType = 1;
                }
                else if (buttondata.Count(o => o.Id == id) > 0)
                {
                    itemType = 2;
                }
                if (itemType > 0)
                {
                    RoleAuthorize ra = new RoleAuthorize
                    {
                        ObjectId    = entity.Id,
                        ObjectType  = 1,
                        ItemId      = id,
                        ItemType    = itemType,
                        CreatorTime = DateTime.Now
                    };

                    ras.Add(ra);
                }
            }

            UnitWork.Update <Role>(entity);
            UnitWork.BatchAdd <RoleAuthorize>(ras.ToArray());
            UnitWork.Delete <RoleAuthorize>(o => deleteIds.Contains(o.Id));
            UnitWork.Save();

            if (currentUser != null)
            {
                await OperateLogApp.InsertLogAsync <Role>(currentUser, "修改角色", entity);
            }
            await RemoveCacheAsync(entity.Id);

            return(ResultDto <Role> .Suc(entity));
        }