Example #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);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="curUser"></param>
        /// <returns></returns>
        public async Task <List <ModuleButton> > GetButtonList(long roleId, bool bSys)
        {
            List <ModuleButton> data;

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

                if (data == null)
                {
                    data = await ModuleButtonApp.GetSaasModuleListAsync(new ModuleButtonOption { IsEnabled = true });

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

                if (data == null)
                {
                    data = new List <ModuleButton>();
                    var buttondata = await ModuleButtonApp.GetSaasModuleListAsync(new ModuleButtonOption { IsEnabled = true });

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

                    foreach (var item in authorizedata)
                    {
                        ModuleButton moduleButtonEntity = buttondata.Find(t => t.Id == item.ItemId);
                        if (moduleButtonEntity != null)
                        {
                            data.Add(moduleButtonEntity);
                        }
                    }
                    await Cache.AddAsync(roleId.ToString(), data, new TimeSpan(0, 30, 0), "buttons");
                }
            }
            return(data.OrderBy(t => t.SortCode).ToList());
        }
Example #3
0
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task <R <Role> > CreateAsync(Role entity, List <long> permissionIds, CurrentUser currentUser)
        {
            entity.Id          = entity.CreateId();
            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
                    };
                    ra.Id = ra.CreateId();
                    ras.Add(ra);
                }
            }

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


            if (currentUser != null)
            {
                await OperateLogApp.InsertLogAsync <Role>(currentUser, "添加角色", entity);
            }
            return(R <Role> .Suc(entity));
        }