Beispiel #1
0
        /// <summary>
        /// Authorize role
        /// </summary>
        /// <param name="role">User Role record</param>
        /// <param name="user">User</param>
        /// <returns>true - authorized; otherwise, false</returns>        
        public virtual bool AuthorizeRole(UserRole role, User user)
        {
            if (role == null)
                return false;

            if (user == null)
                return false;

            var userRoles = user.UserRoles.Where(ur => ur.Active);
            foreach (var userRole in userRoles)
                if (userRole.SystemName.Equals(role.SystemName, StringComparison.InvariantCultureIgnoreCase))
                    return true;

            return false;
        }
Beispiel #2
0
        /// <summary>
        /// Install permissions
        /// </summary>
        /// <param name="permissionProvider">Permission provider</param>
        public virtual void Install(IPermissionProvider permissionProvider)
        {
            //install new permissions
            var permissions = permissionProvider.GetPermissions();
            foreach (var permission in permissions)
            {
                var permission1 = GetBySystemName(permission.SystemName);
                if (permission1 == null)
                {
                    //new permission (install it)
                    permission1 = new PermissionRecord()
                    {
                        Name       = permission.Name,
                        SystemName = permission.SystemName,
                        Category   = permission.Category,
                    };

                    //default customer role mappings
                    var defaultPermissions = permissionProvider.GetDefaultPermissions();
                    foreach (var defaultPermission in defaultPermissions)
                    {
                        var userRole = userService.GetUserRoleBySystemName(defaultPermission.UserRoleSystemName);
                        if (userRole == null)
                        {
                            //new role (save it)
                            userRole = new UserRole()
                            {
                                Name       = defaultPermission.UserRoleSystemName,
                                Active     = true,
                                SystemName = defaultPermission.UserRoleSystemName
                            };
                            userService.InsertUserRole(userRole);
                        }

                        var defaultMappingProvided = (from p in defaultPermission.PermissionRecords
                                                      where p.SystemName == permission1.SystemName
                                                      select p).Any();
                        var mappingExists = (from p in userRole.PermissionRecords
                                             where p.SystemName == permission1.SystemName
                                             select p).Any();
                        if (defaultMappingProvided && !mappingExists)
                        {
                            permission1.UserRoles.Add(userRole);
                        }
                    }

                    //save new permission
                    Insert(permission1);
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Authorize role
 /// </summary>
 /// <param name="role">User Role record</param>
 /// <returns>true - authorized; otherwise, false</returns>        
 public virtual bool AuthorizeRole(UserRole role)
 {
     return AuthorizeRole(role, workContext.CurrentUser);
 }