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-02-29 20:31:21 +0000
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
985d2f1c2cee6a1192df388c70154ccee9114795
985d2f1c
1 parent
7f587237
Tied entity restriction system into userCan checks
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
14 deletions
app/Http/Controllers/Controller.php
app/Services/RestrictionService.php
app/helpers.php
app/Http/Controllers/Controller.php
View file @
985d2f1
...
...
@@ -68,7 +68,7 @@ abstract class Controller extends BaseController
protected
function
showPermissionError
()
{
Session
::
flash
(
'error'
,
trans
(
'errors.permission'
));
$response
=
request
()
->
wantsJson
()
?
response
()
->
json
([
'error'
=>
trans
(
'errors.permissionJson'
)],
403
)
:
redirect
(
'/'
,
403
);
$response
=
request
()
->
wantsJson
()
?
response
()
->
json
([
'error'
=>
trans
(
'errors.permissionJson'
)],
403
)
:
redirect
(
'/'
);
throw
new
HttpResponseException
(
$response
);
}
...
...
@@ -93,10 +93,8 @@ abstract class Controller extends BaseController
*/
protected
function
checkOwnablePermission
(
$permission
,
Ownable
$ownable
)
{
$permissionBaseName
=
strtolower
(
$permission
)
.
'-'
;
if
(
userCan
(
$permissionBaseName
.
'all'
))
return
true
;
if
(
userCan
(
$permissionBaseName
.
'own'
)
&&
$ownable
->
createdBy
->
id
===
$this
->
currentUser
->
id
)
return
true
;
$this
->
showPermissionError
();
if
(
userCan
(
$permission
,
$ownable
))
return
true
;
return
$this
->
showPermissionError
();
}
/**
...
...
app/Services/RestrictionService.php
View file @
985d2f1
<?php
namespace
BookStack\Services
;
use
BookStack\Entity
;
class
RestrictionService
{
...
...
@@ -12,8 +14,24 @@ class RestrictionService
*/
public
function
__construct
()
{
$this
->
userRoles
=
auth
()
->
user
()
->
roles
->
pluck
(
'id'
);
$this
->
isAdmin
=
auth
()
->
user
()
->
hasRole
(
'admin'
);
$user
=
auth
()
->
user
();
$this
->
userRoles
=
$user
?
auth
()
->
user
()
->
roles
->
pluck
(
'id'
)
:
false
;
$this
->
isAdmin
=
$user
?
auth
()
->
user
()
->
hasRole
(
'admin'
)
:
false
;
}
public
function
checkIfEntityRestricted
(
Entity
$entity
,
$action
)
{
if
(
$this
->
isAdmin
)
return
true
;
$this
->
currentAction
=
$action
;
$baseQuery
=
$entity
->
where
(
'id'
,
'='
,
$entity
->
id
);
if
(
$entity
->
isA
(
'page'
))
{
return
$this
->
pageRestrictionQuery
(
$baseQuery
)
->
count
()
>
0
;
}
elseif
(
$entity
->
isA
(
'chapter'
))
{
return
$this
->
chapterRestrictionQuery
(
$baseQuery
)
->
count
()
>
0
;
}
elseif
(
$entity
->
isA
(
'book'
))
{
return
$this
->
bookRestrictionQuery
(
$baseQuery
)
->
count
()
>
0
;
}
return
false
;
}
/**
...
...
@@ -184,25 +202,25 @@ class RestrictionService
if
(
$this
->
isAdmin
)
return
$query
;
$this
->
currentAction
=
'view'
;
$tableDetails
=
[
'tableName'
=>
$tableName
,
'entityIdColumn'
=>
$entityIdColumn
,
'entityTypeColumn'
=>
$entityTypeColumn
];
return
$query
->
where
(
function
(
$query
)
use
(
$tableDetails
)
{
return
$query
->
where
(
function
(
$query
)
use
(
$tableDetails
)
{
$query
->
where
(
function
(
$query
)
use
(
&
$tableDetails
)
{
$query
->
where
(
$tableDetails
[
'entityTypeColumn'
],
'='
,
'BookStack\Page'
)
->
whereExists
(
function
(
$query
)
use
(
&
$tableDetails
)
{
$query
->
select
(
'*'
)
->
from
(
'pages'
)
->
whereRaw
(
'pages.id='
.
$tableDetails
[
'tableName'
]
.
'.'
.
$tableDetails
[
'entityIdColumn'
])
$query
->
select
(
'*'
)
->
from
(
'pages'
)
->
whereRaw
(
'pages.id='
.
$tableDetails
[
'tableName'
]
.
'.'
.
$tableDetails
[
'entityIdColumn'
])
->
where
(
function
(
$query
)
{
$this
->
pageRestrictionQuery
(
$query
);
});
});
})
->
orWhere
(
function
(
$query
)
use
(
&
$tableDetails
)
{
$query
->
where
(
$tableDetails
[
'entityTypeColumn'
],
'='
,
'BookStack\Book'
)
->
whereExists
(
function
(
$query
)
use
(
&
$tableDetails
)
{
$query
->
select
(
'*'
)
->
from
(
'books'
)
->
whereRaw
(
'books.id='
.
$tableDetails
[
'tableName'
]
.
'.'
.
$tableDetails
[
'entityIdColumn'
])
$query
->
select
(
'*'
)
->
from
(
'books'
)
->
whereRaw
(
'books.id='
.
$tableDetails
[
'tableName'
]
.
'.'
.
$tableDetails
[
'entityIdColumn'
])
->
where
(
function
(
$query
)
{
$this
->
bookRestrictionQuery
(
$query
);
});
});
})
->
orWhere
(
function
(
$query
)
use
(
&
$tableDetails
)
{
$query
->
where
(
$tableDetails
[
'entityTypeColumn'
],
'='
,
'BookStack\Chapter'
)
->
whereExists
(
function
(
$query
)
use
(
&
$tableDetails
)
{
$query
->
select
(
'*'
)
->
from
(
'chapters'
)
->
whereRaw
(
'chapters.id='
.
$tableDetails
[
'tableName'
]
.
'.'
.
$tableDetails
[
'entityIdColumn'
])
$query
->
select
(
'*'
)
->
from
(
'chapters'
)
->
whereRaw
(
'chapters.id='
.
$tableDetails
[
'tableName'
]
.
'.'
.
$tableDetails
[
'entityIdColumn'
])
->
where
(
function
(
$query
)
{
$this
->
chapterRestrictionQuery
(
$query
);
});
...
...
app/helpers.php
View file @
985d2f1
...
...
@@ -43,8 +43,18 @@ function userCan($permission, \BookStack\Ownable $ownable = null)
return
auth
()
->
user
()
&&
auth
()
->
user
()
->
can
(
$permission
);
}
// Check permission on ownable item
$permissionBaseName
=
strtolower
(
$permission
)
.
'-'
;
if
(
userCan
(
$permissionBaseName
.
'all'
))
return
true
;
if
(
userCan
(
$permissionBaseName
.
'own'
)
&&
$ownable
->
createdBy
->
id
===
auth
()
->
user
()
->
id
)
return
true
;
return
false
;
$hasPermission
=
false
;
if
(
auth
()
->
user
()
->
can
(
$permissionBaseName
.
'all'
))
$hasPermission
=
true
;
if
(
auth
()
->
user
()
->
can
(
$permissionBaseName
.
'own'
)
&&
$ownable
->
createdBy
->
id
===
auth
()
->
user
()
->
id
)
$hasPermission
=
true
;
if
(
!
$ownable
instanceof
\BookStack\Entity
)
return
$hasPermission
;
// Check restrictions on the entitiy
$restrictionService
=
app
(
'BookStack\Services\RestrictionService'
);
$explodedPermission
=
explode
(
'-'
,
$permission
);
$action
=
end
(
$explodedPermission
);
$hasAccess
=
$restrictionService
->
checkIfEntityRestricted
(
$ownable
,
$action
);
return
$hasAccess
&&
$hasPermission
;
}
\ No newline at end of file
...
...
Please
register
or
sign in
to post a comment