/// <summary>
        /// Creates a permission type and populates permissions for all roles and users
        /// </summary>
        private void CreatePermissionType(PermissionTypes permissionType)
        {
            var permissionTypeInDb = new PermissionType
            {
                Id          = permissionType.ToInt(),
                Name        = GetName(permissionType),
                Description = GetDescription(permissionType),
            };

            Uow.PermissionTypes.Create(permissionTypeInDb);

            // Ensure the permission type is written with the expected id
            if (permissionTypeInDb.Id != permissionType.ToInt())
            {
                throw new Exception($"Permission type {permissionType} was auto-created with id {permissionTypeInDb.Id} but should have been id {permissionType.ToInt()}. This will need to be manually repaired.");
            }

            CreateRoleAndUserPermissions(permissionType);
        }
        /// <summary>
        /// Creates a permission on all roles and users for the given permission type, with a named list of roles
        /// having the permission enabled, and all other roles having the permission disabled
        /// </summary>
        public void CreatePermissionForAllRolesAndUsers(PermissionTypes permissionType, Roles[] rolesWithPermissionEnabled)
        {
            using (var transaction = new TransactionScope())
            {
                var enabledRoleIds = rolesWithPermissionEnabled.Select(x => x.ToInt()).ToList();
                var permissions    = new List <Permission>();

                // Add for roles
                var roles = Uow.Roles.GetAll();
                foreach (var role in roles)
                {
                    //if (role.Id == Roles.Student.ToInt())
                    //    continue;

                    permissions.Add(new Permission
                    {
                        PermissionTypeId = permissionType.ToInt(),
                        RoleId           = role.Id,
                        Enabled          = enabledRoleIds.Contains(role.Id),
                    });
                }

                // Add for users, based on their roles
                var users = Uow.Users.GetAll();
                foreach (var user in users)
                {
                    permissions.Add(new Permission
                    {
                        PermissionTypeId = permissionType.ToInt(),
                        UserId           = user.Id,
                        Enabled          = enabledRoleIds.Contains(user.RoleId),
                    });
                }

                // Commit to db, since this can be heavy we need to use an optimised process
                new BatchedEntityCreator <Permission>().Create(permissions);

                transaction.Complete();
            }
        }
Beispiel #3
0
        public static bool IsUserAllowed(int userID, int contentID, PermissionTypes permissionType, ContentTypes contentType)
        {
            int permissionTypeID = permissionType.ToInt();
            int contentTypeID    = contentType.ToInt();

            return(db.Permissions.Any(
                       p =>
                       p.PermissionTypeID >= permissionTypeID &&
                       p.ContentTypeID == contentTypeID &&
                       p.ContentID == contentID &&
                       p.GrantedUserID == userID
                       ));
        }
 public List <Permission> GetAllForPermissionType(PermissionTypes permissionType)
 {
     return(GetAllForPermissionType(permissionType.ToInt()));
 }