Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Зуев Егор
/
wiki.dev
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Authored by
Dan Brown
2016-04-20 21:37:57 +0100
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
ea287ebf86e2cafcab167eb048a04b6f80146d16
ea287ebf
1 parent
043cdeaf
Started creation of intermediate permission table
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
140 additions
and
1 deletions
app/EntityPermission.php
app/Services/RestrictionService.php
database/migrations/2016_04_20_192649_create_entity_permissions_table.php
app/EntityPermission.php
0 → 100644
View file @
ea287eb
<?php
namespace
BookStack
;
use
Illuminate\Database\Eloquent\Model
;
class
EntityPermission
extends
Model
{
public
$timestamps
=
false
;
/**
* Get the role that this points to.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public
function
role
()
{
return
$this
->
belongsTo
(
Role
::
class
);
}
/**
* Get the entity this points to.
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public
function
entity
()
{
return
$this
->
morphOne
(
Entity
::
class
,
'entity'
);
}
}
app/Services/RestrictionService.php
View file @
ea287eb
<?php
namespace
BookStack\Services
;
use
BookStack\Book
;
use
BookStack\Chapter
;
use
BookStack\Entity
;
use
BookStack\EntityPermission
;
use
BookStack\Page
;
use
BookStack\Permission
;
use
BookStack\Role
;
use
Illuminate\Database\Eloquent\Collection
;
class
RestrictionService
{
...
...
@@ -10,14 +17,84 @@ class RestrictionService
protected
$currentAction
;
protected
$currentUser
;
public
$book
;
public
$chapter
;
public
$page
;
protected
$entityPermission
;
protected
$role
;
protected
$permission
;
/**
* RestrictionService constructor.
* @param EntityPermission $entityPermission
* @param Book $book
* @param Chapter $chapter
* @param Page $page
* @param Role $role
* @param Permission $permission
*/
public
function
__construct
()
public
function
__construct
(
EntityPermission
$entityPermission
,
Book
$book
,
Chapter
$chapter
,
Page
$page
,
Role
$role
,
Permission
$permission
)
{
$this
->
currentUser
=
auth
()
->
user
();
$this
->
userRoles
=
$this
->
currentUser
?
$this
->
currentUser
->
roles
->
pluck
(
'id'
)
:
[];
$this
->
isAdmin
=
$this
->
currentUser
?
$this
->
currentUser
->
hasRole
(
'admin'
)
:
false
;
$this
->
entityPermission
=
$entityPermission
;
$this
->
role
=
$role
;
$this
->
permission
=
$permission
;
$this
->
book
=
$book
;
$this
->
chapter
=
$chapter
;
$this
->
page
=
$page
;
}
/**
* Re-generate all entity permission from scratch.
*/
public
function
buildEntityPermissions
()
{
$this
->
entityPermission
->
truncate
();
// Get all roles (Should be the most limited dimension)
$roles
=
$this
->
role
->
load
(
'permissions'
)
->
all
();
// Chunk through all books
$this
->
book
->
chunk
(
500
,
function
(
$books
)
use
(
$roles
)
{
$this
->
createManyEntityPermissions
(
$books
,
$roles
);
});
// Chunk through all chapters
$this
->
chapter
->
chunk
(
500
,
function
(
$books
)
use
(
$roles
)
{
$this
->
createManyEntityPermissions
(
$books
,
$roles
);
});
// Chunk through all pages
$this
->
page
->
chunk
(
500
,
function
(
$books
)
use
(
$roles
)
{
$this
->
createManyEntityPermissions
(
$books
,
$roles
);
});
}
/**
* Create & Save entity permissions for many entities and permissions.
* @param Collection $entities
* @param Collection $roles
*/
protected
function
createManyEntityPermissions
(
$entities
,
$roles
)
{
$entityPermissions
=
[];
foreach
(
$entities
as
$entity
)
{
foreach
(
$roles
as
$role
)
{
$entityPermissions
[]
=
$this
->
createEntityPermission
(
$entity
,
$role
);
}
}
$this
->
entityPermission
->
insert
(
$entityPermissions
);
}
protected
function
createEntityPermissionData
(
Entity
$entity
,
Role
$role
)
{
// TODO - Check the permission values and return an EntityPermission
}
/**
...
...
database/migrations/2016_04_20_192649_create_entity_permissions_table.php
0 → 100644
View file @
ea287eb
<?php
use
Illuminate\Database\Schema\Blueprint
;
use
Illuminate\Database\Migrations\Migration
;
class
CreateEntityPermissionsTable
extends
Migration
{
/**
* Run the migrations.
*
* @return void
*/
public
function
up
()
{
Schema
::
create
(
'entity_permissions'
,
function
(
Blueprint
$table
)
{
$table
->
increments
(
'id'
);
$table
->
integer
(
'role_id'
);
$table
->
string
(
'entity_type'
);
$table
->
integer
(
'entity_id'
);
$table
->
string
(
'action'
);
$table
->
boolean
(
'has_permission'
)
->
default
(
false
);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public
function
down
()
{
Schema
::
drop
(
'entity_permissions'
);
}
}
Please
register
or
sign in
to post a comment