Exemple #1
0
        public static List <DdlVm.DdlOption> LoadQs(long userId, string parameters, string searchTerm, int pageSize, int pageNum, out long count)
        {
            var blObject = new BlModule();

            var serializer = new JavaScriptSerializer();
            var dict       = serializer.Deserialize <Dictionary <string, object> >(parameters);
            var isActive   = CheckEmpty.Boolean(ref dict, "isActive");

            var predicate = PredicateBuilder.True <Module>();

            if (isActive)
            {
                predicate = predicate.And(c => c.Status == "A");
            }

            if (CheckEmpty.String(searchTerm) != "")
            {
                var tokens = searchTerm.Tokens();
                foreach (var token in tokens)
                {
                    var predicate2 = PredicateBuilder.False <Module>();
                    predicate2 = predicate2.Or(m => m.Id.Contains(token));
                    predicate  = predicate.And(predicate2);
                }
            }

            var items = blObject.LoadPaging(userId, predicate, pageSize, (pageNum - 1), out count);

            return(items.Select(FormatForQs).ToList());
        }
Exemple #2
0
        private string GetRolePermissions(long userId, long roleId)
        {
            var blModule = new BlModule();
            var modules  = blModule.LoadAll(userId).ToList();

            var toRetModules = (from m in modules
                                select new TreeItemVm
            {
                id = "M_" + m.Id,
                parent = "#",
                text = m.Id,
                icon = "fa fa-folder colorMain ",
                state = "{\"opened\": \"true\"}",
                li_attr = "{\"class\" : \"form-control-label\"}",
                a_attr = "{\"class\": \"no_checkbox\"}"
            }).ToList();


            //Get all modules permissions and set is active for assigned ones
            var blPermission = new BlPermission();
            var permissions  = blPermission.LoadAll(userId).ToList();

            permissions.ForEach(p => p.IsActive = p.Roles.Where(r => r.Id == roleId).Any());

            //Get is active from parent and set them as disabled
            var parentRolesIds = LoadParents(userId, LoadSingle(userId, roleId).SingleItemAsList()).Where(role => role.Id != roleId).Select(parentRole => parentRole.Id);

            permissions.ForEach(p => p.IsActiveInherited = p.Roles.Where(r => parentRolesIds.Contains(r.Id)).Any());

            var toRetPermissions = (from p in permissions
                                    select new TreeItemVm
            {
                id = p.Id.ToString(),
                parent = "M_" + p.Module.Id,
                text = p.Code,
                icon = "fa fa-gear " + (p.IsActive ? "colorGreen" : "colorRed"),
                state = "{\"selected\": \"" + (p.IsActive || p.IsActiveInherited ? "true" : "false") + "\", \"disabled\": \"" + (p.IsActiveInherited ? "true" : "false") + "\"}",
                li_attr = "{\"class\" : \"form-control-label " + (p.IsActiveInherited ? "jstree-disabled" : "") + "\"}"
            }).ToList();

            toRetModules.AddRange(toRetPermissions);
            var toRet = new JavaScriptSerializer().Serialize(toRetModules);

            return(toRet);
        }
        private void PreSave(long userId, ref Permission toSave, Enumerations.ActionMode action, string roles = "")
        {
            if (action == Enumerations.ActionMode.Add)
            {
                toSave.Module    = BlModule.LoadSingle(userId, toSave.Module.Id);
                toSave.EntryDate = BlCommon.GetServerDateTime();
            }
            toSave.UserId = userId;

            if (CheckEmpty.String(roles) != "")
            {
                toSave.Roles.Clear();
                foreach (var roleId in roles.Split(','))
                {
                    toSave.Roles.Add(BlRole.LoadSingle(userId, Convert.ToInt64(roleId)));
                }
            }
        }
        public IEnumerable <Permission> LoadByModuleAndRole(long userId, string moduleId, List <Role> roles, bool onlyActive = false)
        {
            var predicate = PredicateBuilder.True <Permission>();

            predicate = predicate.And(e => e.Module.Id == moduleId);
            if (onlyActive)
            {
                predicate = predicate.And(e => e.Module.Status == "A");
            }
            var permissions = _repository.LoadSearch(predicate).ToList();

            //Create modules and access permission if it does not exist
            if (!permissions.Any())
            {
                var user = BlUser.LoadSingle(userId);
                //Create module if does not exist
                if (BlModule.LoadSingle(userId, moduleId) == null)
                {
                    var newModule = new Module
                    {
                        Id          = moduleId,
                        Description = "N/A",
                        Author      = user.Id,
                        UserId      = user.Id,
                        Status      = "A"
                    };
                    var blModule = new BlModule();
                    blModule.Create(userId, newModule);
                }
                //Create access permission
                var newPermission = new Permission
                {
                    Module = BlModule.LoadSingle(userId, moduleId),
                    Code   = "Access",
                    Roles  = BlRole.LoadSingle(userId, Constants.FullAdminRole).SingleItemAsEnumerable().ToList(),
                    Status = "A"
                };
                Create(userId, newPermission);
            }

            var tmpPermissions = new Permission[permissions.Count];

            permissions.CopyTo(tmpPermissions);

            //Remove permission outside the roles
            foreach (var permission in tmpPermissions)
            {
                var keepFlag = false;
                foreach (var role in roles)
                {
                    if (permission.Roles.Select(r => r.Id).Contains(role.Id))
                    {
                        keepFlag = true;
                        break;
                    }
                }
                if (!keepFlag)
                {
                    permissions.Remove(permission);
                }
            }

            return(permissions);
        }