Ejemplo n.º 1
0
        public async Task <IActionResult> GetResource([FromBody] ObjGetResourceModel obj)
        {
            try
            {
                List <AdResourcePermission> result = new List <AdResourcePermission>();
                foreach (var funcIdS in obj.ListFuncId)
                {
                    var funcId = funcIdS;
                    List <AdPermission> listPermission = new List <AdPermission>();
                    foreach (var groupId in obj.ListGUserId)
                    {
                        var pms = await _context.AdPermissions.Where(x => x.ApplicationCode == obj.AppCode && x.FunctionCode == funcId && x.GroupUserCode == groupId).ToListAsync();

                        listPermission.AddRange(pms);
                    }
                    var funcRes = await _context.AdPrivileges.Include(x => x.Resource).Where(x => x.FunctionCode == funcId).OrderBy(o => o.Resource.Title).ToListAsync();

                    var query = from fr in funcRes
                                select new AdResourcePermission
                    {
                        Id            = fr.PrivilegeId,
                        Code          = fr.Resource.ResourceCode,
                        Title         = fr.Resource.Title,
                        Description   = fr.Resource.Description,
                        Api           = fr.Resource.Api,
                        Path          = fr.Resource.Path,
                        Ord           = fr.Resource.Ord,
                        ParentCode    = fr.Resource.ParentCode,
                        FunctionCode  = fr.FunctionCode,
                        HasPermission = listPermission.Any(x => x.ResourceCode == fr.ResourceCode)
                    };
                    result.AddRange(query);
                }
                return(Json(result));
            }
            catch (Exception ex)
            {
                JMessage objex = new JMessage()
                {
                    Error = true, Object = ex
                };
                return(Json(objex));
            }
        }
Ejemplo n.º 2
0
        public async Task <JsonResult> GetResource([FromBody] ObjGetResourceModel obj)
        {
            List <AdResourcePermission> result = new List <AdResourcePermission>();

            try
            {
                var listPermissionDefault = await _context.AdPermissions.Include(i => i.Function).Where(x => x.ApplicationCode == obj.AppCode && x.UserId == null && x.RoleId == obj.RoleId && obj.ListGUserId.Any(y => y == x.GroupUserCode)).ToListAsync();

                var listPrivileges = await _context.AdPrivileges.Include(x => x.Function).Include(x => x.Resource).Where(x => x.Resource.Status && obj.ListFuncId.Any(y => y == x.FunctionCode)).ToListAsync();

                if (listPrivileges.Count > 0)
                {
                    var groupFunction = listPrivileges.GroupBy(g => g.Function).OrderBy(o => o.Key.ParentCode).ThenBy(t => t.Key.FunctionCode).ToList();
                    if (groupFunction.Count > 0)
                    {
                        foreach (var groupfunc in groupFunction)
                        {
                            var function = groupfunc.Key;
                            // Get all resource of function
                            var listPrivilegeOfFunction = listPrivileges.Where(x => x.FunctionCode == function.FunctionCode).ToList();
                            if (listPrivilegeOfFunction.Count > 0)
                            {
                                var defaultFunction = new AdResourcePermission();
                                defaultFunction.Id            = function.FunctionId;
                                defaultFunction.Code          = function.FunctionCode;
                                defaultFunction.Title         = function.Title;
                                defaultFunction.Description   = function.Description;
                                defaultFunction.Ord           = function.Ord;
                                defaultFunction.ParentCode    = function.ParentCode;
                                defaultFunction.FunctionCode  = function.FunctionCode;
                                defaultFunction.FunctionName  = function.Title;
                                defaultFunction.HasPermission = true;
                                defaultFunction.IsFunction    = true;
                                result.Add(defaultFunction); // Add first function

                                var query = from pr in listPrivilegeOfFunction
                                            join gfr in groupfunc on pr.ResourceCode equals gfr.ResourceCode into grpFunc
                                            from fr in grpFunc.DefaultIfEmpty()
                                            select new AdResourcePermission
                                {
                                    Id            = pr.PrivilegeId,
                                    Code          = pr.Resource.ResourceCode,
                                    Title         = pr.Resource.Title,
                                    Description   = pr.Resource.Description,
                                    Api           = pr.Resource.Api,
                                    Path          = pr.Resource.Path,
                                    Ord           = pr.Resource.Ord,
                                    Style         = pr.Resource.Style,
                                    Scope         = pr.Resource.Scope,
                                    ParentCode    = pr.Resource.ParentCode,
                                    FunctionCode  = pr.FunctionCode,
                                    FunctionName  = pr.Function.Title,
                                    IsFunction    = false,
                                    HasPermission = !obj.IsMultiple && listPermissionDefault.Any(x => x.FunctionCode == pr.FunctionCode && x.ResourceCode == pr.ResourceCode)
                                };
                                result.AddRange(query);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                JMessage objex = new JMessage()
                {
                    Error = true, Object = ex
                };
            }

            return(Json(result));
        }