Merge branch 'master' into release
This commit is contained in:
		
						commit
						2b25bf6f3b
					
				| 
						 | 
				
			
			@ -49,6 +49,7 @@ class RegeneratePermissions extends Command
 | 
			
		|||
        $connection = \DB::getDefaultConnection();
 | 
			
		||||
        if ($this->option('database') !== null) {
 | 
			
		||||
            \DB::setDefaultConnection($this->option('database'));
 | 
			
		||||
            $this->permissionService->setConnection(\DB::connection($this->option('database')));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->permissionService->buildJointPermissions();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -44,6 +44,7 @@ class RegenerateSearch extends Command
 | 
			
		|||
        $connection = \DB::getDefaultConnection();
 | 
			
		||||
        if ($this->option('database') !== null) {
 | 
			
		||||
            \DB::setDefaultConnection($this->option('database'));
 | 
			
		||||
            $this->searchService->setConnection(\DB::connection($this->option('database')));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->searchService->indexAllEntities();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -94,17 +94,6 @@ class Entity extends Ownable
 | 
			
		|||
            ->where('action', '=', $action)->count() > 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Check if this entity has live (active) restrictions in place.
 | 
			
		||||
     * @param $role_id
 | 
			
		||||
     * @param $action
 | 
			
		||||
     * @return bool
 | 
			
		||||
     */
 | 
			
		||||
    public function hasActiveRestriction($role_id, $action)
 | 
			
		||||
    {
 | 
			
		||||
        return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the entity jointPermissions this is connected to.
 | 
			
		||||
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
 | 
			
		||||
| 
						 | 
				
			
			@ -176,5 +165,11 @@ class Entity extends Ownable
 | 
			
		|||
     */
 | 
			
		||||
    public function entityRawQuery(){return '';}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the url of this entity
 | 
			
		||||
     * @param $path
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    public function getUrl($path){return '/';}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
<?php namespace BookStack\Http\Controllers;
 | 
			
		||||
 | 
			
		||||
use Activity;
 | 
			
		||||
use BookStack\Book;
 | 
			
		||||
use BookStack\Repos\EntityRepo;
 | 
			
		||||
use BookStack\Repos\UserRepo;
 | 
			
		||||
use BookStack\Services\ExportService;
 | 
			
		||||
| 
						 | 
				
			
			@ -207,13 +208,12 @@ class BookController extends Controller
 | 
			
		|||
 | 
			
		||||
        // Add activity for books
 | 
			
		||||
        foreach ($sortedBooks as $bookId) {
 | 
			
		||||
            /** @var Book $updatedBook */
 | 
			
		||||
            $updatedBook = $this->entityRepo->getById('book', $bookId);
 | 
			
		||||
            $this->entityRepo->buildJointPermissionsForBook($updatedBook);
 | 
			
		||||
            Activity::add($updatedBook, 'book_sort', $updatedBook->id);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Update permissions on changed models
 | 
			
		||||
        if (count($updatedModels) === 0) $this->entityRepo->buildJointPermissions($updatedModels);
 | 
			
		||||
 | 
			
		||||
        return redirect($book->getUrl());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -46,7 +46,7 @@ class HomeController extends Controller
 | 
			
		|||
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
 | 
			
		||||
     */
 | 
			
		||||
    public function getTranslations() {
 | 
			
		||||
        $locale = trans()->getLocale();
 | 
			
		||||
        $locale = app()->getLocale();
 | 
			
		||||
        $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
 | 
			
		||||
        if (cache()->has($cacheKey) && config('app.env') !== 'development') {
 | 
			
		||||
            $resp = cache($cacheKey);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,17 @@ class Localization
 | 
			
		|||
    public function handle($request, Closure $next)
 | 
			
		||||
    {
 | 
			
		||||
        $defaultLang = config('app.locale');
 | 
			
		||||
        $locale = setting()->getUser(user(), 'language', $defaultLang);
 | 
			
		||||
        if (user()->isDefault()) {
 | 
			
		||||
            $locale = $defaultLang;
 | 
			
		||||
            $availableLocales = config('app.locales');
 | 
			
		||||
            foreach ($request->getLanguages() as $lang) {
 | 
			
		||||
                if (!in_array($lang, $availableLocales)) continue;
 | 
			
		||||
                $locale = $lang;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            $locale = setting()->getUser(user(), 'language', $defaultLang);
 | 
			
		||||
        }
 | 
			
		||||
        app()->setLocale($locale);
 | 
			
		||||
        Carbon::setLocale($locale);
 | 
			
		||||
        return $next($request);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -533,11 +533,11 @@ class EntityRepo
 | 
			
		|||
 | 
			
		||||
    /**
 | 
			
		||||
     * Alias method to update the book jointPermissions in the PermissionService.
 | 
			
		||||
     * @param Collection $collection collection on entities
 | 
			
		||||
     * @param Book $book
 | 
			
		||||
     */
 | 
			
		||||
    public function buildJointPermissions(Collection $collection)
 | 
			
		||||
    public function buildJointPermissionsForBook(Book $book)
 | 
			
		||||
    {
 | 
			
		||||
        $this->permissionService->buildJointPermissionsForEntities($collection);
 | 
			
		||||
        $this->permissionService->buildJointPermissionsForEntity($book);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			@ -730,6 +730,7 @@ class EntityRepo
 | 
			
		|||
        if ($chapter) $page->chapter_id = $chapter->id;
 | 
			
		||||
 | 
			
		||||
        $book->pages()->save($page);
 | 
			
		||||
        $page = $this->page->find($page->id);
 | 
			
		||||
        $this->permissionService->buildJointPermissionsForEntity($page);
 | 
			
		||||
        return $page;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,7 @@
 | 
			
		|||
use BookStack\Book;
 | 
			
		||||
use BookStack\Chapter;
 | 
			
		||||
use BookStack\Entity;
 | 
			
		||||
use BookStack\EntityPermission;
 | 
			
		||||
use BookStack\JointPermission;
 | 
			
		||||
use BookStack\Ownable;
 | 
			
		||||
use BookStack\Page;
 | 
			
		||||
| 
						 | 
				
			
			@ -10,6 +11,7 @@ use BookStack\Role;
 | 
			
		|||
use BookStack\User;
 | 
			
		||||
use Illuminate\Database\Connection;
 | 
			
		||||
use Illuminate\Database\Eloquent\Builder;
 | 
			
		||||
use Illuminate\Database\Query\Builder as QueryBuilder;
 | 
			
		||||
use Illuminate\Support\Collection;
 | 
			
		||||
 | 
			
		||||
class PermissionService
 | 
			
		||||
| 
						 | 
				
			
			@ -28,22 +30,25 @@ class PermissionService
 | 
			
		|||
 | 
			
		||||
    protected $jointPermission;
 | 
			
		||||
    protected $role;
 | 
			
		||||
    protected $entityPermission;
 | 
			
		||||
 | 
			
		||||
    protected $entityCache;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * PermissionService constructor.
 | 
			
		||||
     * @param JointPermission $jointPermission
 | 
			
		||||
     * @param EntityPermission $entityPermission
 | 
			
		||||
     * @param Connection $db
 | 
			
		||||
     * @param Book $book
 | 
			
		||||
     * @param Chapter $chapter
 | 
			
		||||
     * @param Page $page
 | 
			
		||||
     * @param Role $role
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct(JointPermission $jointPermission, Connection $db, Book $book, Chapter $chapter, Page $page, Role $role)
 | 
			
		||||
    public function __construct(JointPermission $jointPermission, EntityPermission $entityPermission, Connection $db, Book $book, Chapter $chapter, Page $page, Role $role)
 | 
			
		||||
    {
 | 
			
		||||
        $this->db = $db;
 | 
			
		||||
        $this->jointPermission = $jointPermission;
 | 
			
		||||
        $this->entityPermission = $entityPermission;
 | 
			
		||||
        $this->role = $role;
 | 
			
		||||
        $this->book = $book;
 | 
			
		||||
        $this->chapter = $chapter;
 | 
			
		||||
| 
						 | 
				
			
			@ -51,6 +56,15 @@ class PermissionService
 | 
			
		|||
        // TODO - Update so admin still goes through filters
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Set the database connection
 | 
			
		||||
     * @param Connection $connection
 | 
			
		||||
     */
 | 
			
		||||
    public function setConnection(Connection $connection)
 | 
			
		||||
    {
 | 
			
		||||
        $this->db = $connection;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Prepare the local entity cache and ensure it's empty
 | 
			
		||||
     */
 | 
			
		||||
| 
						 | 
				
			
			@ -133,22 +147,48 @@ class PermissionService
 | 
			
		|||
        $this->readyEntityCache();
 | 
			
		||||
 | 
			
		||||
        // Get all roles (Should be the most limited dimension)
 | 
			
		||||
        $roles = $this->role->with('permissions')->get();
 | 
			
		||||
        $roles = $this->role->with('permissions')->get()->all();
 | 
			
		||||
 | 
			
		||||
        // Chunk through all books
 | 
			
		||||
        $this->book->with('permissions')->chunk(500, function ($books) use ($roles) {
 | 
			
		||||
            $this->createManyJointPermissions($books, $roles);
 | 
			
		||||
        $this->bookFetchQuery()->chunk(5, function ($books) use ($roles) {
 | 
			
		||||
            $this->buildJointPermissionsForBooks($books, $roles);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        // Chunk through all chapters
 | 
			
		||||
        $this->chapter->with('book', 'permissions')->chunk(500, function ($chapters) use ($roles) {
 | 
			
		||||
            $this->createManyJointPermissions($chapters, $roles);
 | 
			
		||||
        });
 | 
			
		||||
    /**
 | 
			
		||||
     * Get a query for fetching a book with it's children.
 | 
			
		||||
     * @return QueryBuilder
 | 
			
		||||
     */
 | 
			
		||||
    protected function bookFetchQuery()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
 | 
			
		||||
            $query->select(['id', 'restricted', 'created_by', 'book_id']);
 | 
			
		||||
        }, 'pages'  => function($query) {
 | 
			
		||||
            $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
 | 
			
		||||
        }]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        // Chunk through all pages
 | 
			
		||||
        $this->page->with('book', 'chapter', 'permissions')->chunk(500, function ($pages) use ($roles) {
 | 
			
		||||
            $this->createManyJointPermissions($pages, $roles);
 | 
			
		||||
        });
 | 
			
		||||
    /**
 | 
			
		||||
     * Build joint permissions for an array of books
 | 
			
		||||
     * @param Collection $books
 | 
			
		||||
     * @param array $roles
 | 
			
		||||
     * @param bool $deleteOld
 | 
			
		||||
     */
 | 
			
		||||
    protected function buildJointPermissionsForBooks($books, $roles, $deleteOld = false) {
 | 
			
		||||
        $entities = clone $books;
 | 
			
		||||
 | 
			
		||||
        /** @var Book $book */
 | 
			
		||||
        foreach ($books->all() as $book) {
 | 
			
		||||
            foreach ($book->getRelation('chapters') as $chapter) {
 | 
			
		||||
                $entities->push($chapter);
 | 
			
		||||
            }
 | 
			
		||||
            foreach ($book->getRelation('pages') as $page) {
 | 
			
		||||
                $entities->push($page);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($deleteOld) $this->deleteManyJointPermissionsForEntities($entities->all());
 | 
			
		||||
        $this->createManyJointPermissions($entities, $roles);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			@ -157,18 +197,22 @@ class PermissionService
 | 
			
		|||
     */
 | 
			
		||||
    public function buildJointPermissionsForEntity(Entity $entity)
 | 
			
		||||
    {
 | 
			
		||||
        $roles = $this->role->get();
 | 
			
		||||
        $entities = collect([$entity]);
 | 
			
		||||
 | 
			
		||||
        $entities = [$entity];
 | 
			
		||||
        if ($entity->isA('book')) {
 | 
			
		||||
            $entities = $entities->merge($entity->chapters);
 | 
			
		||||
            $entities = $entities->merge($entity->pages);
 | 
			
		||||
        } elseif ($entity->isA('chapter')) {
 | 
			
		||||
            $entities = $entities->merge($entity->pages);
 | 
			
		||||
            $books = $this->bookFetchQuery()->where('id', '=', $entity->id)->get();
 | 
			
		||||
            $this->buildJointPermissionsForBooks($books, $this->role->newQuery()->get(), true);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $entities[] = $entity->book;
 | 
			
		||||
        if ($entity->isA('page') && $entity->chapter_id) $entities[] = $entity->chapter;
 | 
			
		||||
        if ($entity->isA('chapter')) {
 | 
			
		||||
            foreach ($entity->pages as $page) {
 | 
			
		||||
                $entities[] = $page;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        $this->deleteManyJointPermissionsForEntities($entities);
 | 
			
		||||
        $this->createManyJointPermissions($entities, $roles);
 | 
			
		||||
        $this->buildJointPermissionsForEntities(collect($entities));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			@ -177,8 +221,8 @@ class PermissionService
 | 
			
		|||
     */
 | 
			
		||||
    public function buildJointPermissionsForEntities(Collection $entities)
 | 
			
		||||
    {
 | 
			
		||||
        $roles = $this->role->get();
 | 
			
		||||
        $this->deleteManyJointPermissionsForEntities($entities);
 | 
			
		||||
        $roles = $this->role->newQuery()->get();
 | 
			
		||||
        $this->deleteManyJointPermissionsForEntities($entities->all());
 | 
			
		||||
        $this->createManyJointPermissions($entities, $roles);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -188,23 +232,12 @@ class PermissionService
 | 
			
		|||
     */
 | 
			
		||||
    public function buildJointPermissionForRole(Role $role)
 | 
			
		||||
    {
 | 
			
		||||
        $roles = collect([$role]);
 | 
			
		||||
 | 
			
		||||
        $roles = [$role];
 | 
			
		||||
        $this->deleteManyJointPermissionsForRoles($roles);
 | 
			
		||||
 | 
			
		||||
        // Chunk through all books
 | 
			
		||||
        $this->book->with('permissions')->chunk(500, function ($books) use ($roles) {
 | 
			
		||||
            $this->createManyJointPermissions($books, $roles);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // Chunk through all chapters
 | 
			
		||||
        $this->chapter->with('book', 'permissions')->chunk(500, function ($books) use ($roles) {
 | 
			
		||||
            $this->createManyJointPermissions($books, $roles);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // Chunk through all pages
 | 
			
		||||
        $this->page->with('book', 'chapter', 'permissions')->chunk(500, function ($books) use ($roles) {
 | 
			
		||||
            $this->createManyJointPermissions($books, $roles);
 | 
			
		||||
        $this->bookFetchQuery()->chunk(5, function ($books) use ($roles) {
 | 
			
		||||
            $this->buildJointPermissionsForBooks($books, $roles);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -223,9 +256,10 @@ class PermissionService
 | 
			
		|||
     */
 | 
			
		||||
    protected function deleteManyJointPermissionsForRoles($roles)
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($roles as $role) {
 | 
			
		||||
            $role->jointPermissions()->delete();
 | 
			
		||||
        }
 | 
			
		||||
        $roleIds = array_map(function($role) {
 | 
			
		||||
            return $role->id;
 | 
			
		||||
        }, $roles);
 | 
			
		||||
        $this->jointPermission->newQuery()->whereIn('id', $roleIds)->delete();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			@ -244,53 +278,88 @@ class PermissionService
 | 
			
		|||
    protected function deleteManyJointPermissionsForEntities($entities)
 | 
			
		||||
    {
 | 
			
		||||
        if (count($entities) === 0) return;
 | 
			
		||||
        $query = $this->jointPermission->newQuery();
 | 
			
		||||
            foreach ($entities as $entity) {
 | 
			
		||||
                $query->orWhere(function($query) use ($entity) {
 | 
			
		||||
                    $query->where('entity_id', '=', $entity->id)
 | 
			
		||||
                        ->where('entity_type', '=', $entity->getMorphClass());
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
        $this->db->transaction(function() use ($entities) {
 | 
			
		||||
 | 
			
		||||
            foreach (array_chunk($entities, 1000) as $entityChunk) {
 | 
			
		||||
                $query = $this->db->table('joint_permissions');
 | 
			
		||||
                foreach ($entityChunk as $entity) {
 | 
			
		||||
                    $query->orWhere(function(QueryBuilder $query) use ($entity) {
 | 
			
		||||
                        $query->where('entity_id', '=', $entity->id)
 | 
			
		||||
                            ->where('entity_type', '=', $entity->getMorphClass());
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
                $query->delete();
 | 
			
		||||
            }
 | 
			
		||||
        $query->delete();
 | 
			
		||||
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create & Save entity jointPermissions for many entities and jointPermissions.
 | 
			
		||||
     * @param Collection $entities
 | 
			
		||||
     * @param Collection $roles
 | 
			
		||||
     * @param array $roles
 | 
			
		||||
     */
 | 
			
		||||
    protected function createManyJointPermissions($entities, $roles)
 | 
			
		||||
    {
 | 
			
		||||
        $this->readyEntityCache();
 | 
			
		||||
        $jointPermissions = [];
 | 
			
		||||
 | 
			
		||||
        // Fetch Entity Permissions and create a mapping of entity restricted statuses
 | 
			
		||||
        $entityRestrictedMap = [];
 | 
			
		||||
        $permissionFetch = $this->entityPermission->newQuery();
 | 
			
		||||
        foreach ($entities as $entity) {
 | 
			
		||||
            $entityRestrictedMap[$entity->getMorphClass() . ':' . $entity->id] = boolval($entity->getRawAttribute('restricted'));
 | 
			
		||||
            $permissionFetch->orWhere(function($query) use ($entity) {
 | 
			
		||||
                $query->where('restrictable_id', '=', $entity->id)->where('restrictable_type', '=', $entity->getMorphClass());
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        $permissions = $permissionFetch->get();
 | 
			
		||||
 | 
			
		||||
        // Create a mapping of explicit entity permissions
 | 
			
		||||
        $permissionMap = [];
 | 
			
		||||
        foreach ($permissions as $permission) {
 | 
			
		||||
            $key = $permission->restrictable_type . ':' . $permission->restrictable_id . ':' . $permission->role_id . ':' . $permission->action;
 | 
			
		||||
            $isRestricted = $entityRestrictedMap[$permission->restrictable_type . ':' . $permission->restrictable_id];
 | 
			
		||||
            $permissionMap[$key] = $isRestricted;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Create a mapping of role permissions
 | 
			
		||||
        $rolePermissionMap = [];
 | 
			
		||||
        foreach ($roles as $role) {
 | 
			
		||||
            foreach ($role->getRelationValue('permissions') as $permission) {
 | 
			
		||||
                $rolePermissionMap[$role->getRawAttribute('id') . ':' . $permission->getRawAttribute('name')] = true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Create Joint Permission Data
 | 
			
		||||
        foreach ($entities as $entity) {
 | 
			
		||||
            foreach ($roles as $role) {
 | 
			
		||||
                foreach ($this->getActions($entity) as $action) {
 | 
			
		||||
                    $jointPermissions[] = $this->createJointPermissionData($entity, $role, $action);
 | 
			
		||||
                    $jointPermissions[] = $this->createJointPermissionData($entity, $role, $action, $permissionMap, $rolePermissionMap);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        $this->jointPermission->insert($jointPermissions);
 | 
			
		||||
 | 
			
		||||
        $this->db->transaction(function() use ($jointPermissions) {
 | 
			
		||||
            foreach (array_chunk($jointPermissions, 1000) as $jointPermissionChunk) {
 | 
			
		||||
                $this->db->table('joint_permissions')->insert($jointPermissionChunk);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the actions related to an entity.
 | 
			
		||||
     * @param $entity
 | 
			
		||||
     * @param Entity $entity
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    protected function getActions($entity)
 | 
			
		||||
    protected function getActions(Entity $entity)
 | 
			
		||||
    {
 | 
			
		||||
        $baseActions = ['view', 'update', 'delete'];
 | 
			
		||||
 | 
			
		||||
        if ($entity->isA('chapter')) {
 | 
			
		||||
            $baseActions[] = 'page-create';
 | 
			
		||||
        } else if ($entity->isA('book')) {
 | 
			
		||||
            $baseActions[] = 'page-create';
 | 
			
		||||
            $baseActions[] = 'chapter-create';
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
         return $baseActions;
 | 
			
		||||
        if ($entity->isA('chapter') || $entity->isA('book')) $baseActions[] = 'page-create';
 | 
			
		||||
        if ($entity->isA('book')) $baseActions[] = 'chapter-create';
 | 
			
		||||
        return $baseActions;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			@ -298,14 +367,16 @@ class PermissionService
 | 
			
		|||
     * for a particular action.
 | 
			
		||||
     * @param Entity $entity
 | 
			
		||||
     * @param Role $role
 | 
			
		||||
     * @param $action
 | 
			
		||||
     * @param string $action
 | 
			
		||||
     * @param array $permissionMap
 | 
			
		||||
     * @param array $rolePermissionMap
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    protected function createJointPermissionData(Entity $entity, Role $role, $action)
 | 
			
		||||
    protected function createJointPermissionData(Entity $entity, Role $role, $action, $permissionMap, $rolePermissionMap)
 | 
			
		||||
    {
 | 
			
		||||
        $permissionPrefix = (strpos($action, '-') === false ? ($entity->getType() . '-') : '') . $action;
 | 
			
		||||
        $roleHasPermission = $role->hasPermission($permissionPrefix . '-all');
 | 
			
		||||
        $roleHasPermissionOwn = $role->hasPermission($permissionPrefix . '-own');
 | 
			
		||||
        $roleHasPermission = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-all']);
 | 
			
		||||
        $roleHasPermissionOwn = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-own']);
 | 
			
		||||
        $explodedAction = explode('-', $action);
 | 
			
		||||
        $restrictionAction = end($explodedAction);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -313,54 +384,46 @@ class PermissionService
 | 
			
		|||
            return $this->createJointPermissionDataArray($entity, $role, $action, true, true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($entity->isA('book')) {
 | 
			
		||||
 | 
			
		||||
            if (!$entity->restricted) {
 | 
			
		||||
                return $this->createJointPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn);
 | 
			
		||||
            } else {
 | 
			
		||||
                $hasAccess = $entity->hasActiveRestriction($role->id, $restrictionAction);
 | 
			
		||||
                return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } elseif ($entity->isA('chapter')) {
 | 
			
		||||
 | 
			
		||||
            if (!$entity->restricted) {
 | 
			
		||||
                $book = $this->getBook($entity->book_id);
 | 
			
		||||
                $hasExplicitAccessToBook = $book->hasActiveRestriction($role->id, $restrictionAction);
 | 
			
		||||
                $hasPermissiveAccessToBook = !$book->restricted;
 | 
			
		||||
                return $this->createJointPermissionDataArray($entity, $role, $action,
 | 
			
		||||
                    ($hasExplicitAccessToBook || ($roleHasPermission && $hasPermissiveAccessToBook)),
 | 
			
		||||
                    ($hasExplicitAccessToBook || ($roleHasPermissionOwn && $hasPermissiveAccessToBook)));
 | 
			
		||||
            } else {
 | 
			
		||||
                $hasAccess = $entity->hasActiveRestriction($role->id, $restrictionAction);
 | 
			
		||||
                return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } elseif ($entity->isA('page')) {
 | 
			
		||||
 | 
			
		||||
            if (!$entity->restricted) {
 | 
			
		||||
                $book = $this->getBook($entity->book_id);
 | 
			
		||||
                $hasExplicitAccessToBook = $book->hasActiveRestriction($role->id, $restrictionAction);
 | 
			
		||||
                $hasPermissiveAccessToBook = !$book->restricted;
 | 
			
		||||
 | 
			
		||||
                $chapter = $this->getChapter($entity->chapter_id);
 | 
			
		||||
                $hasExplicitAccessToChapter = $chapter && $chapter->hasActiveRestriction($role->id, $restrictionAction);
 | 
			
		||||
                $hasPermissiveAccessToChapter = $chapter && !$chapter->restricted;
 | 
			
		||||
                $acknowledgeChapter = ($chapter && $chapter->restricted);
 | 
			
		||||
 | 
			
		||||
                $hasExplicitAccessToParents = $acknowledgeChapter ? $hasExplicitAccessToChapter : $hasExplicitAccessToBook;
 | 
			
		||||
                $hasPermissiveAccessToParents = $acknowledgeChapter ? $hasPermissiveAccessToChapter : $hasPermissiveAccessToBook;
 | 
			
		||||
 | 
			
		||||
                return $this->createJointPermissionDataArray($entity, $role, $action,
 | 
			
		||||
                    ($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)),
 | 
			
		||||
                    ($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents))
 | 
			
		||||
                );
 | 
			
		||||
            } else {
 | 
			
		||||
                $hasAccess = $entity->hasRestriction($role->id, $action);
 | 
			
		||||
                return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        if ($entity->restricted) {
 | 
			
		||||
            $hasAccess = $this->mapHasActiveRestriction($permissionMap, $entity, $role, $restrictionAction);
 | 
			
		||||
            return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($entity->isA('book')) {
 | 
			
		||||
            return $this->createJointPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // For chapters and pages, Check if explicit permissions are set on the Book.
 | 
			
		||||
        $book = $this->getBook($entity->book_id);
 | 
			
		||||
        $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $book, $role, $restrictionAction);
 | 
			
		||||
        $hasPermissiveAccessToParents = !$book->restricted;
 | 
			
		||||
 | 
			
		||||
        // For pages with a chapter, Check if explicit permissions are set on the Chapter
 | 
			
		||||
        if ($entity->isA('page') && $entity->chapter_id !== 0) {
 | 
			
		||||
            $chapter = $this->getChapter($entity->chapter_id);
 | 
			
		||||
            $hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapter->restricted;
 | 
			
		||||
            if ($chapter->restricted) {
 | 
			
		||||
                $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $chapter, $role, $restrictionAction);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this->createJointPermissionDataArray($entity, $role, $action,
 | 
			
		||||
            ($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)),
 | 
			
		||||
            ($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents))
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Check for an active restriction in an entity map.
 | 
			
		||||
     * @param $entityMap
 | 
			
		||||
     * @param Entity $entity
 | 
			
		||||
     * @param Role $role
 | 
			
		||||
     * @param $action
 | 
			
		||||
     * @return bool
 | 
			
		||||
     */
 | 
			
		||||
    protected function mapHasActiveRestriction($entityMap, Entity $entity, Role $role, $action) {
 | 
			
		||||
        $key = $entity->getMorphClass() . ':' . $entity->getRawAttribute('id') . ':' . $role->getRawAttribute('id') . ':' . $action;
 | 
			
		||||
        return isset($entityMap[$key]) ? $entityMap[$key] : false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			@ -375,11 +438,10 @@ class PermissionService
 | 
			
		|||
     */
 | 
			
		||||
    protected function createJointPermissionDataArray(Entity $entity, Role $role, $action, $permissionAll, $permissionOwn)
 | 
			
		||||
    {
 | 
			
		||||
        $entityClass = get_class($entity);
 | 
			
		||||
        return [
 | 
			
		||||
            'role_id'            => $role->getRawAttribute('id'),
 | 
			
		||||
            'entity_id'          => $entity->getRawAttribute('id'),
 | 
			
		||||
            'entity_type'        => $entityClass,
 | 
			
		||||
            'entity_type'        => $entity->getMorphClass(),
 | 
			
		||||
            'action'             => $action,
 | 
			
		||||
            'has_permission'     => $permissionAll,
 | 
			
		||||
            'has_permission_own' => $permissionOwn,
 | 
			
		||||
| 
						 | 
				
			
			@ -476,7 +538,7 @@ class PermissionService
 | 
			
		|||
     * @param integer $book_id
 | 
			
		||||
     * @param bool $filterDrafts
 | 
			
		||||
     * @param bool $fetchPageContent
 | 
			
		||||
     * @return \Illuminate\Database\Query\Builder
 | 
			
		||||
     * @return QueryBuilder
 | 
			
		||||
     */
 | 
			
		||||
    public function bookChildrenQuery($book_id, $filterDrafts = false, $fetchPageContent = false) {
 | 
			
		||||
        $pageSelect = $this->db->table('pages')->selectRaw($this->page->entityRawQuery($fetchPageContent))->where('book_id', '=', $book_id)->where(function($query) use ($filterDrafts) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -50,6 +50,15 @@ class SearchService
 | 
			
		|||
        $this->permissionService = $permissionService;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Set the database connection
 | 
			
		||||
     * @param Connection $connection
 | 
			
		||||
     */
 | 
			
		||||
    public function setConnection(Connection $connection)
 | 
			
		||||
    {
 | 
			
		||||
        $this->db = $connection;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Search all entities in the system.
 | 
			
		||||
     * @param string $searchString
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -58,6 +58,7 @@ return [
 | 
			
		|||
    */
 | 
			
		||||
 | 
			
		||||
    'locale' => env('APP_LANG', 'en'),
 | 
			
		||||
    'locales' => ['en', 'de', 'es', 'fr', 'nl', 'pt_BR', 'sk'],
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
    |--------------------------------------------------------------------------
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,6 +28,12 @@ class DummyContentSeeder extends Seeder
 | 
			
		|||
                $book->pages()->saveMany($pages);
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        $largeBook = factory(\BookStack\Book::class)->create(['name' => 'Large book' . str_random(10), 'created_by' => $user->id, 'updated_by' => $user->id]);
 | 
			
		||||
        $pages = factory(\BookStack\Page::class, 200)->make(['created_by' => $user->id, 'updated_by' => $user->id]);
 | 
			
		||||
        $chapters = factory(\BookStack\Chapter::class, 50)->make(['created_by' => $user->id, 'updated_by' => $user->id]);
 | 
			
		||||
        $largeBook->pages()->saveMany($pages);
 | 
			
		||||
        $largeBook->chapters()->saveMany($chapters);
 | 
			
		||||
 | 
			
		||||
        app(\BookStack\Services\PermissionService::class)->buildJointPermissions();
 | 
			
		||||
        app(\BookStack\Services\SearchService::class)->indexAllEntities();
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,6 +43,8 @@ Once done you can run `phpunit` in the application root directory to run all tes
 | 
			
		|||
## Translations
 | 
			
		||||
 | 
			
		||||
As part of BookStack v0.14 support for translations has been built in. All text strings can be found in the `resources/lang` folder where each language option has its own folder. To add a new language you should copy the `en` folder to an new folder (eg. `fr` for french) then go through and translate all text strings in those files, leaving the keys and file-names intact. If a language string is missing then the `en` translation will be used. To show the language option in the user preferences language drop-down you will need to add your language to the options found at the bottom of the `resources/lang/en/settings.php` file. A system-wide language can also be set in the `.env` file like so: `APP_LANG=en`.
 | 
			
		||||
 | 
			
		||||
You will also need to add the language to the `locales` array in the `config/app.php` file.
 | 
			
		||||
 
 | 
			
		||||
 Some strings have colon-prefixed variables in such as `:userName`. Leave these values as they are as they will be replaced at run-time.
 | 
			
		||||
 
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -215,7 +215,7 @@ module.exports = function (ngApp, events) {
 | 
			
		|||
        }
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    const md = new MarkdownIt();
 | 
			
		||||
    const md = new MarkdownIt({html: true});
 | 
			
		||||
    md.use(mdTasksLists, {label: true});
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -166,7 +166,7 @@ return [
 | 
			
		|||
        'start_a' => ':count usuarios han comenzado a editar esta página',
 | 
			
		||||
        'start_b' => ':userName ha comenzado a editar esta página',
 | 
			
		||||
        'time_a' => 'desde que las página fue actualizada',
 | 
			
		||||
        'time_b' => 'en los Ãltimos :minCount minutos',
 | 
			
		||||
        'time_b' => 'en los últimos :minCount minutos',
 | 
			
		||||
        'message' => ':start :time. Ten cuidado de no sobreescribir los cambios del otro usuario',
 | 
			
		||||
    ],
 | 
			
		||||
    'pages_draft_discarded' => 'Borrador descartado, el editor ha sido actualizado con el contenido de la página actual',
 | 
			
		||||
| 
						 | 
				
			
			@ -189,7 +189,7 @@ return [
 | 
			
		|||
    'attachments_set_link' => 'Setear Link',
 | 
			
		||||
    'attachments_delete_confirm' => 'Haga click en borrar nuevamente para confirmar que quiere borrar este adjunto.',
 | 
			
		||||
    'attachments_dropzone' => 'Arrastre ficheros aquío haga click aquípara adjuntar un fichero',
 | 
			
		||||
    'attachments_no_files' => 'NingÃn fichero ha sido adjuntado',
 | 
			
		||||
    'attachments_no_files' => 'Ningún fichero ha sido adjuntado',
 | 
			
		||||
    'attachments_explain_link' => 'Ud. puede agregar un link o si lo prefiere puede agregar un fichero. Esto puede ser un link a otra página o un link a un fichero en la nube.',
 | 
			
		||||
    'attachments_link_name' => 'Nombre de Link',
 | 
			
		||||
    'attachment_link' => 'Link adjunto',
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
<?php namespace Tests;
 | 
			
		||||
 | 
			
		||||
use BookStack\Role;
 | 
			
		||||
use BookStack\Services\PermissionService;
 | 
			
		||||
use Illuminate\Contracts\Console\Kernel;
 | 
			
		||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
 | 
			
		||||
use Laravel\BrowserKitTesting\TestCase;
 | 
			
		||||
| 
						 | 
				
			
			@ -105,11 +106,9 @@ abstract class BrowserKitTest extends TestCase
 | 
			
		|||
    {
 | 
			
		||||
        if ($updaterUser === false) $updaterUser = $creatorUser;
 | 
			
		||||
        $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
 | 
			
		||||
        $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
 | 
			
		||||
        $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
 | 
			
		||||
        $book->chapters()->saveMany([$chapter]);
 | 
			
		||||
        $chapter->pages()->saveMany([$page]);
 | 
			
		||||
        $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
 | 
			
		||||
        $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
 | 
			
		||||
        $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
 | 
			
		||||
        $restrictionService = $this->app[PermissionService::class];
 | 
			
		||||
        $restrictionService->buildJointPermissionsForEntity($book);
 | 
			
		||||
        return [
 | 
			
		||||
            'book' => $book,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -42,7 +42,7 @@ class EntitySearchTest extends TestCase
 | 
			
		|||
 | 
			
		||||
    public function test_book_search()
 | 
			
		||||
    {
 | 
			
		||||
        $book = \BookStack\Book::all()->first();
 | 
			
		||||
        $book = \BookStack\Book::first();
 | 
			
		||||
        $page = $book->pages->last();
 | 
			
		||||
        $chapter = $book->chapters->last();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
<?php namespace Tests;
 | 
			
		||||
 | 
			
		||||
use BookStack\Role;
 | 
			
		||||
use BookStack\Tag;
 | 
			
		||||
use BookStack\Page;
 | 
			
		||||
use BookStack\Services\PermissionService;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,6 +14,23 @@ class LanguageTest extends TestCase
 | 
			
		|||
        $this->langs = array_diff(scandir(resource_path('lang')), ['..', '.']);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_locales_config_key_set_properly()
 | 
			
		||||
    {
 | 
			
		||||
        $configLocales = config('app.locales');
 | 
			
		||||
        sort($configLocales);
 | 
			
		||||
        sort($this->langs);
 | 
			
		||||
        $this->assertTrue(implode(':', $this->langs) === implode(':', $configLocales), 'app.locales configuration variable matches found lang files');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_correct_language_if_not_logged_in()
 | 
			
		||||
    {
 | 
			
		||||
        $loginReq = $this->get('/login');
 | 
			
		||||
        $loginReq->assertSee('Log In');
 | 
			
		||||
 | 
			
		||||
        $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
 | 
			
		||||
        $loginPageFrenchReq->assertSee('Se Connecter');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_js_endpoint_for_each_language()
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -226,6 +226,7 @@ class RestrictionsTest extends BrowserKitTest
 | 
			
		|||
            ->type('test content', 'html')
 | 
			
		||||
            ->press('Save Page')
 | 
			
		||||
            ->seePageIs($chapter->book->getUrl() . '/page/test-page');
 | 
			
		||||
 | 
			
		||||
        $this->visit($chapterUrl)->seeInElement('.action-buttons', 'New Page');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,8 @@
 | 
			
		|||
<?php namespace Tests;
 | 
			
		||||
 | 
			
		||||
use BookStack\Repos\PermissionsRepo;
 | 
			
		||||
use BookStack\Role;
 | 
			
		||||
 | 
			
		||||
class RolesTest extends BrowserKitTest
 | 
			
		||||
{
 | 
			
		||||
    protected $user;
 | 
			
		||||
| 
						 | 
				
			
			@ -34,11 +37,11 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
    /**
 | 
			
		||||
     * Create a new basic role for testing purposes.
 | 
			
		||||
     * @param array $permissions
 | 
			
		||||
     * @return static
 | 
			
		||||
     * @return Role
 | 
			
		||||
     */
 | 
			
		||||
    protected function createNewRole($permissions = [])
 | 
			
		||||
    {
 | 
			
		||||
        $permissionRepo = app('BookStack\Repos\PermissionsRepo');
 | 
			
		||||
        $permissionRepo = app(PermissionsRepo::class);
 | 
			
		||||
        $roleData = factory(\BookStack\Role::class)->make()->toArray();
 | 
			
		||||
        $roleData['permissions'] = array_flip($permissions);
 | 
			
		||||
        return $permissionRepo->saveNewRole($roleData);
 | 
			
		||||
| 
						 | 
				
			
			@ -107,16 +110,16 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
 | 
			
		||||
    public function test_manage_user_permission()
 | 
			
		||||
    {
 | 
			
		||||
        $this->actingAs($this->user)->visit('/')->visit('/settings/users')
 | 
			
		||||
        $this->actingAs($this->user)->visit('/settings/users')
 | 
			
		||||
            ->seePageIs('/');
 | 
			
		||||
        $this->giveUserPermissions($this->user, ['users-manage']);
 | 
			
		||||
        $this->actingAs($this->user)->visit('/')->visit('/settings/users')
 | 
			
		||||
        $this->actingAs($this->user)->visit('/settings/users')
 | 
			
		||||
            ->seePageIs('/settings/users');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_user_roles_manage_permission()
 | 
			
		||||
    {
 | 
			
		||||
        $this->actingAs($this->user)->visit('/')->visit('/settings/roles')
 | 
			
		||||
        $this->actingAs($this->user)->visit('/settings/roles')
 | 
			
		||||
            ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/');
 | 
			
		||||
        $this->giveUserPermissions($this->user, ['user-roles-manage']);
 | 
			
		||||
        $this->actingAs($this->user)->visit('/settings/roles')
 | 
			
		||||
| 
						 | 
				
			
			@ -126,10 +129,10 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
 | 
			
		||||
    public function test_settings_manage_permission()
 | 
			
		||||
    {
 | 
			
		||||
        $this->actingAs($this->user)->visit('/')->visit('/settings')
 | 
			
		||||
        $this->actingAs($this->user)->visit('/settings')
 | 
			
		||||
            ->seePageIs('/');
 | 
			
		||||
        $this->giveUserPermissions($this->user, ['settings-manage']);
 | 
			
		||||
        $this->actingAs($this->user)->visit('/')->visit('/settings')
 | 
			
		||||
        $this->actingAs($this->user)->visit('/settings')
 | 
			
		||||
            ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -181,27 +184,26 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
     * @param string $permission
 | 
			
		||||
     * @param array $accessUrls Urls that are only accessible after having the permission
 | 
			
		||||
     * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission
 | 
			
		||||
     * @param null $callback
 | 
			
		||||
     */
 | 
			
		||||
    private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($accessUrls as $url) {
 | 
			
		||||
            $this->actingAs($this->user)->visit('/')->visit($url)
 | 
			
		||||
            $this->actingAs($this->user)->visit($url)
 | 
			
		||||
                ->seePageIs('/');
 | 
			
		||||
        }
 | 
			
		||||
        foreach ($visibles as $url => $text) {
 | 
			
		||||
            $this->actingAs($this->user)->visit('/')->visit($url)
 | 
			
		||||
            $this->actingAs($this->user)->visit($url)
 | 
			
		||||
                ->dontSeeInElement('.action-buttons',$text);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->giveUserPermissions($this->user, [$permission]);
 | 
			
		||||
 | 
			
		||||
        foreach ($accessUrls as $url) {
 | 
			
		||||
            $this->actingAs($this->user)->visit('/')->visit($url)
 | 
			
		||||
            $this->actingAs($this->user)->visit($url)
 | 
			
		||||
                ->seePageIs($url);
 | 
			
		||||
        }
 | 
			
		||||
        foreach ($visibles as $url => $text) {
 | 
			
		||||
            $this->actingAs($this->user)->visit('/')->visit($url)
 | 
			
		||||
            $this->actingAs($this->user)->visit($url)
 | 
			
		||||
                ->see($text);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -391,8 +393,8 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
 | 
			
		||||
    public function test_page_create_own_permissions()
 | 
			
		||||
    {
 | 
			
		||||
        $book = \BookStack\Book::take(1)->get()->first();
 | 
			
		||||
        $chapter = \BookStack\Chapter::take(1)->get()->first();
 | 
			
		||||
        $book = \BookStack\Book::first();
 | 
			
		||||
        $chapter = \BookStack\Chapter::first();
 | 
			
		||||
 | 
			
		||||
        $entities = $this->createEntityChainBelongingToUser($this->user);
 | 
			
		||||
        $ownBook = $entities['book'];
 | 
			
		||||
| 
						 | 
				
			
			@ -405,7 +407,7 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
        $accessUrls = [$createUrl, $createUrlChapter];
 | 
			
		||||
 | 
			
		||||
        foreach ($accessUrls as $url) {
 | 
			
		||||
            $this->actingAs($this->user)->visit('/')->visit($url)
 | 
			
		||||
            $this->actingAs($this->user)->visit($url)
 | 
			
		||||
                ->seePageIs('/');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -417,7 +419,7 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
        $this->giveUserPermissions($this->user, ['page-create-own']);
 | 
			
		||||
 | 
			
		||||
        foreach ($accessUrls as $index => $url) {
 | 
			
		||||
            $this->actingAs($this->user)->visit('/')->visit($url);
 | 
			
		||||
            $this->actingAs($this->user)->visit($url);
 | 
			
		||||
            $expectedUrl = \BookStack\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
 | 
			
		||||
            $this->seePageIs($expectedUrl);
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -449,7 +451,7 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
        $accessUrls = [$createUrl, $createUrlChapter];
 | 
			
		||||
 | 
			
		||||
        foreach ($accessUrls as $url) {
 | 
			
		||||
            $this->actingAs($this->user)->visit('/')->visit($url)
 | 
			
		||||
            $this->actingAs($this->user)->visit($url)
 | 
			
		||||
                ->seePageIs('/');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -461,7 +463,7 @@ class RolesTest extends BrowserKitTest
 | 
			
		|||
        $this->giveUserPermissions($this->user, ['page-create-all']);
 | 
			
		||||
 | 
			
		||||
        foreach ($accessUrls as $index => $url) {
 | 
			
		||||
            $this->actingAs($this->user)->visit('/')->visit($url);
 | 
			
		||||
            $this->actingAs($this->user)->visit($url);
 | 
			
		||||
            $expectedUrl = \BookStack\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
 | 
			
		||||
            $this->seePageIs($expectedUrl);
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue