示例#1
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)));
                }
            }
        }
示例#2
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);
        }
示例#3
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);
        }
示例#4
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)));
                }
            }
        }
示例#5
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);
        }
示例#6
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()));
        }