Ejemplo n.º 1
0
        public string LoadAllForTree(long userId)
        {
            var predicate = PredicateBuilder.True <Role>();

            //Exclude full admin
            predicate = predicate.And(e => e.Id != 1);

            var roleTree = LoadSearch(userId, predicate);

            //Get security permissions
            var allowView   = BlPermission.CanDo(userId, Module, "View");
            var allowAdd    = BlPermission.CanDo(userId, Module, "Add");
            var allowEdit   = BlPermission.CanDo(userId, Module, "Edit");
            var allowDelete = BlPermission.CanDo(userId, Module, "Delete");

            //Create return object
            var toRet = new List <TreeItemVm>();

            //Create root node
            var rootNode = new Role {
                Id = 0
            };

            //Add root node
            toRet.AddRange(ConstructTreeNode(userId, rootNode.SingleItemAsEnumerable(), false, allowAdd, false, false));

            //Add menus
            toRet.AddRange(ConstructTreeNode(userId, roleTree.ToList(), allowView, allowAdd, allowEdit, allowDelete));

            return(new JavaScriptSerializer().Serialize(toRet));
        }
Ejemplo n.º 2
0
        public LoginVm Authenticate(LoginVm model)
        {
            var hashedPass = Cryptography.ComputeToHash(model.Password);

            if (hashedPass == null)
            {
                throw new BusinessException("InvalidLogin");
            }

            var userRepository = new UserRepository();
            var predicate      = PredicateBuilder.True <User>();

            predicate = predicate.And(u => u.UserName == model.Username);
            var users = userRepository.LoadSearch(predicate);

            //Check if any user have same password
            IStructuralEquatable eqa1 = hashedPass;
            var user = users.FirstOrDefault(u => eqa1.Equals(u.Password, StructuralComparisons.StructuralEqualityComparer));

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

            //Check if user is active
            if (user.IsBlocked)
            {
                throw new BusinessException("UserInactive");
            }

            UpdateTicketValidity(user.Id);

            model.SecurityToken = Cryptography.Encrypt(JsonConvert.SerializeObject(user.Id), true);
            model.Password      = null;
            model.Language      = BlCode.LoadSingle(user.Id, "Language", user.LanguageId.ToUiString()).Value1;
            model.BranchId      = user.BranchId;
            model.BranchName    = BlBranch.GetBranchName(user.Id);
            model.CrossBranches = BlPermission.CanDo(user.Id, "BRANCH", "CrossBranches");
            model.PageSize      = user.PageSize;
            BlLog.Log(user.Id, Module, "LogIn", "UserSuccessfulLogin", new object[] { user.UserName });
            return(model);
        }
Ejemplo n.º 3
0
        public string LoadUserMenu(long userId, bool forEdit)
        {
            var user = BlUser.LoadSingle(userId);

            //Load all active menu items per company
            var predicate = PredicateBuilder.True <Menu>();

            predicate = predicate.And(p => p.Status);

            //Query all entries without a branch or specific for a branch
            var predicate2 = PredicateBuilder.False <Menu>();

            predicate2 = predicate2.Or(p => p.BranchId == user.BranchId);
            predicate2 = predicate2.Or(p => p.BranchId == null);
            predicate  = predicate.And(predicate2);

            var lMenu = LoadSearch(userId, predicate).ToList();

            //Vaidate if all modules exist in permissions
            var lDeniedMenu = new List <Menu>();

            foreach (var menuItem in lMenu.ToList())
            {
                if (menuItem.Module != null)
                {
                    if (!BlPermission.CanDo(userId, menuItem.Module.Id, "Access"))
                    {
                        //Build the denied menu list
                        lDeniedMenu.Add(menuItem);
                    }
                }
            }

            //Remove unauthorized and broken menu items
            lMenu = (from m in lMenu
                     where !(from dm in lDeniedMenu select dm.Id).Contains(m.Id)
                     select m).ToList();

            //Iterate all sub menu items and clear broken entries
            foreach (var menuItem in lMenu.ToList())
            {
                //Remove each node that does not have a module nor children
                if (menuItem.Module == null)
                {
                    if (menuItem.Parent != null && menuItem.DescriptionCode != null)
                    {
                        if (!lMenu.Where(m => m.Id == menuItem.Parent.Id && m.Module != null && m.DescriptionCode != null).Any())
                        {
                            //Check if any children have this item as parent
                            if (!lMenu.Where(m => m.Parent != null).Where(m => m.Parent.Id == menuItem.Id && m.Module != null && m.DescriptionCode != null).Any())
                            {
                                lDeniedMenu.Add(menuItem);
                            }
                        }
                    }
                }
            }

            //Remove unauthorized and broken menu entries
            lMenu = (from m in lMenu
                     where !(from dm in lDeniedMenu select dm.Id).Contains(m.Id)
                     select m).ToList();


            //Iterate all top menu items and clear broken entries
            foreach (var menuItem in lMenu.Where(m => m.Parent == null).ToList())
            {
                if (menuItem.Module == null)
                {
                    //Remove each node that does not have a module nor children
                    if (!lMenu.Where(m => m.Parent != null).Where(m => m.Parent.Id == menuItem.Id && m.DescriptionCode != null).Any())
                    {
                        lDeniedMenu.Add(menuItem);
                    }
                }
            }

            //Remove unauthorized and broken menu items
            lMenu = (from m in lMenu
                     where !(from dm in lDeniedMenu select dm.Id).Contains(m.Id)
                     select m).ToList();

            var menu = BuildMenuChildren(user, lMenu, null, forEdit);

            return(menu);
        }
Ejemplo n.º 4
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()));
        }