Esempio n. 1
0
        public static bool CanDo(long userId, string moduleId, string code)
        {
            //Load user for roles
            var user = BlUser.LoadSingle(userId);

            if (user == null)
            {
                throw new BusinessException("UserNotFound");
            }

            var roles = BlRole.LoadParents(userId, user.Roles.ToList());

            //Exit if full permission case of roles system administrator
            if (roles.Select(r => r.Id).Contains(Constants.FullAdminRole))
            {
                return(true);
            }

            //Load permissions for user roles
            var blPermission = new BlPermission();
            var permissions  = blPermission.LoadByModuleAndRole(user.Id, moduleId, roles.ToList(), true).ToList();

            //Return permission existance
            return(permissions.Where(p => p.Code == code).Any());
        }
Esempio n. 2
0
        public PermissionVm Init(long userId, long?id)
        {
            var toRet = new PermissionVm
            {
                Statuses   = BlCode.LoadTable(userId, "PermissionStatus"),
                ActionMode = Enumerations.ActionMode.Add,
                Permission = new Permission {
                    Status = "A", Module = new Module()
                }
            };

            if (id != null)
            {
                var permission = LoadSingle(userId, Convert.ToInt64(id));
                toRet.Permission = permission;
                //Create roles tree
                var blRole = new BlRole();
                var roles  = blRole.LoadForPermission(userId, permission.Module.Id, permission.Code);

                toRet.RolesTree = new JavaScriptSerializer().Serialize((from r in roles
                                                                        select new TreeItemVm
                {
                    id = r.Id.ToUiString(),
                    parent = r.ParentRole == null ? "#" : r.ParentRole.Id.ToString(),
                    text = r.Code ?? "",
                    icon = "fa fa-users colorMain",
                    state = "{\"checked\": \"" + (r.IsActive ? "true" : "false") + "\"}",
                    li_attr = "{\"class\" : \"form-control-label\"}"
                }));
                toRet.Signature  = BlCommon.GetSignature(toRet.Permission.UserId, toRet.Permission.EntryDate);
                toRet.ActionMode = Enumerations.ActionMode.Edit;
            }

            return(toRet);
        }
Esempio n. 3
0
        private void PreSave(long userId, ref User toSave, Enumerations.ActionMode action, string userRoles = "")
        {
            if (action == Enumerations.ActionMode.Add)
            {
                var defaultPassword = BlCode.LoadSingle(userId, "_System", "DefaultPassword").Value1;
                toSave.Password           = Cryptography.ComputeToHash(defaultPassword);
                toSave.MustChangePassword = true;
                toSave.EntryDate          = BlCommon.GetServerDateTime();
                toSave.LastPasswordUpdate = toSave.EntryDate;
                //toSave.Branch = BlBranch.LoadSingle(userId, toSave.Branch.Id);
            }
            else if (action == Enumerations.ActionMode.Edit)
            {
            }
            toSave.EnteringUserId = userId;
            toSave.Entity         = BlEntity.LoadSingle(userId, toSave.Pin);
            if (CheckEmpty.String(userRoles) != "")
            {
                if (toSave.Roles == null)
                {
                    toSave.Roles = new List <Role>();
                }

                toSave.Roles.Clear();
                foreach (var roleId in userRoles.Split(','))
                {
                    toSave.Roles.Add(BlRole.LoadSingle(userId, Convert.ToInt64(roleId)));
                }
            }
        }
Esempio n. 4
0
        public IEnumerable <Permission> LoadByRole(long userId, long roleId)
        {
            var predicate = PredicateBuilder.True <Permission>();

            predicate = predicate.And(p => p.Roles.Contains(BlRole.LoadSingle(userId, roleId, false)));
            var toRet = LoadSearch(userId, predicate);

            return(toRet);
        }
Esempio n. 5
0
        public UserVm Init(long userId, long?id)
        {
            var callingUser = LoadSingle(userId);

            var toRet = new UserVm
            {
                Branches   = BlBranch.GetLov(userId, true).ToDictionary(i => i.value, i => i.label),
                Languages  = BlCode.LoadTable(userId, "Language", callingUser.LanguageId == (int)Enumerations.Languages.English ? "Value1" : "Value2"),
                PageSizes  = BlCode.LoadTable(userId, "PageSizes", callingUser.LanguageId == (int)Enumerations.Languages.English ? "Value1" : "Value2"),
                ActionMode = Enumerations.ActionMode.Add,
                User       = new User {
                    LanguageId = 1, PageSize = 10
                }
            };

            if (id != null)
            {
                var user = LoadSingle((long)id);
                user.Pin    = user.Entity == null ? user.Pin : user.Entity.Pin;
                user.Entity = null;
                //user.Branch = null;
                toRet.User      = user;
                toRet.Signature = BlCommon.GetSignature(toRet.User.EnteringUserId, toRet.User.EntryDate);
                //Get all roles and check user assigned ones
                var blRole = new BlRole();
                var roles  = blRole.LoadAll(userId).ToList();
                if (user.Roles != null)
                {
                    foreach (var role in user.Roles)
                    {
                        if (roles.Where(r => r.Id == role.Id).Any())
                        {
                            roles.Where(r => r.Id == role.Id).SingleOrDefault().IsActive = true;
                        }
                    }
                }
                toRet.UserRoles = new JavaScriptSerializer().Serialize((from r in roles
                                                                        select new TreeItemVm
                {
                    id = r.Id.ToUiString(),
                    parent = r.ParentRole == null ? "#" : r.ParentRole.Id.ToUiString(),
                    text = r.Code ?? "",
                    icon = "fa fa-users colorMain",
                    state = "{\"checked\": \"" + (r.IsActive ? "true" : "false") + "\"}",
                    li_attr = "{\"class\" : \"form-control-label\"}"
                }));
                //user.Roles = null;
                toRet.ActionMode = Enumerations.ActionMode.Edit;
            }

            return(toRet);
        }
Esempio n. 6
0
        public IEnumerable <User> LoadByRole(long userId, long roleId, bool onlyActive = false)
        {
            var predicate = PredicateBuilder.True <User>();

            predicate = predicate.And(u => u.Roles.Contains(BlRole.LoadSingle(userId, roleId, false)));
            if (onlyActive)
            {
                predicate = predicate.And(u => u.IsBlocked == false);
            }

            var toRet = LoadSearch(userId, predicate);

            return(toRet);
        }
Esempio n. 7
0
        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)));
                }
            }
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        public string GetSecurityString(long userId, string path)
        {
            //CLean path url
            path = path.ToUpper().Replace("../STOCK/APP/", "");
            path = path.ToUpper().Replace("/STOCK/APP/", "");

            var toRetList = new ArrayList();

            //Load the module by path
            var predicate = PredicateBuilder.True <Module>();

            predicate = predicate.And(p => p.Path == path);
            var module = LoadSearch(userId, predicate).FirstOrDefault();

            //Create module if does not exist
            if (module == null)
            {
                using (var tran = new TransactionScope())
                {
                    var systemUser = BlUser.LoadSingle(Constants.SystemUser);
                    var newModule  = new Module
                    {
                        Id          = path.Split('/')[path.Split('/').Length - 1].ToUpper().Replace(".HTML", ""),
                        Path        = path,
                        Description = "N/A",
                        Author      = Constants.SystemUser,
                        Status      = "A"
                    };
                    newModule = Create(userId, newModule);

                    //Create first access permission and assign to system admin
                    var newPermission = new Permission
                    {
                        Module = newModule,
                        Code   = "Access",
                        Status = "A",
                        Roles  =
                            BlRole.LoadSingle(userId, Constants.FullAdminRole).
                            SingleItemAsEnumerable().ToList()
                    };
                    var blPermission = new BlPermission();
                    blPermission.Create(userId, newPermission);
                    tran.Complete();
                }
            }

            //Get module permissions if exists
            if (module != null)
            {
                var blPermission = new BlPermission();
                var permissions  = blPermission.LoadByModule(userId, module.Id, true);

                //Check each permission status
                foreach (var permission in permissions)
                {
                    if (BlPermission.CanDo(userId, module.Id, permission.Code))
                    {
                        toRetList.Add(permission.Code);
                    }
                }
            }

            return(string.Join(":", toRetList.ToArray()));
        }