Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public async Task <bool> ActionValidate(long roleId, string action)
        {
            var authorizeurldata = new List <AuthorizeActionModel>();
            var cachedata        = await Cache.GetAsync <List <AuthorizeActionModel> >(roleId.ToString(), "authorizeurl");

            if (cachedata == null)
            {
                var moduledata = await ModuleApp.GetSaasModuleListAsync();

                var buttondata    = (await ModuleButtonApp.GetSaasModuleListAsync());
                var authorizedata = await RoleAuthorizeRep.Find(o => o.ObjectId == roleId).ToListAsync();

                foreach (var item in authorizedata)
                {
                    if (item.ItemType == 1)
                    {
                        Module module = moduledata.Find(t => t.Id == item.ItemId);
                        if (module != null)
                        {
                            authorizeurldata.Add(new AuthorizeActionModel {
                                Id = module.Id, UrlAddress = module.UrlAddress
                            });
                        }
                    }
                    else if (item.ItemType == 2)
                    {
                        ModuleButton moduleButton = buttondata.Find(t => t.Id == item.ItemId);
                        if (moduleButton != null)
                        {
                            authorizeurldata.Add(new AuthorizeActionModel {
                                Id = moduleButton.ModuleId, UrlAddress = moduleButton.UrlAddress
                            });
                        }
                    }
                }
                await Cache.AddAsync(roleId.ToString(), authorizeurldata, new TimeSpan(0, 30, 0), "authorizeurl");
            }
            else
            {
                authorizeurldata = cachedata;
            }
            foreach (var item in authorizeurldata)
            {
                if (!string.IsNullOrEmpty(item.UrlAddress))
                {
                    string[] url = item.UrlAddress.Split('?');
                    if (url[0].ToLower() == action.ToLower())
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        private async Task <List <long> > Fibonacci(long permissionIds)
        {
            List <long> a          = new List <long>();
            var         moduledata = await ModuleApp.GetSaasModuleListAsync();

            var p = moduledata.Where(o => o.Id == permissionIds).FirstOrDefault();

            if (p.ParentId == 0)
            {
                a.Add(p.Id);
            }
            else
            {
                a.Add(p.Id);
                a.AddRange(await Fibonacci(p.ParentId));
            }
            return(a);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="curUser"></param>
        /// <returns></returns>
        public async Task <List <Module> > GetModuleList(long roleId, bool bSys)
        {
            List <Module> data;

            if (bSys)
            {
                data = await Cache.GetAsync <List <Module> >("sys", "modules");

                if (data == null)
                {
                    data = await ModuleApp.GetSaasModuleListAsync(new ModuleOption { IsEnabled = true });

                    await Cache.AddAsync("sys", data, new TimeSpan(0, 30, 0), "modules");
                }
            }
            else
            {
                data = await Cache.GetAsync <List <Module> >(roleId.ToString(), "modules");

                if (data == null)
                {
                    data = new List <Module>();
                    var moduledata = await ModuleApp.GetSaasModuleListAsync(new ModuleOption { IsEnabled = true });

                    var authorizedata = await RoleAuthorizeRep.Find(o => o.ObjectId == roleId && o.ItemType == 1).ToListAsync();

                    foreach (var item in authorizedata)
                    {
                        Module moduleEntity = moduledata.Find(t => t.Id == item.ItemId);
                        if (moduleEntity != null)
                        {
                            data.Add(moduleEntity);
                        }
                    }
                    await Cache.AddAsync(roleId.ToString(), data, new TimeSpan(0, 30, 0), "modules");
                }
            }
            return(data.OrderBy(t => t.SortCode).ToList());
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
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));
        }