Completed
Push — develop ( dee413...360901 )
by
unknown
14:17
created

ApplicationAccessAssertion   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C assert() 0 27 7
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** */
11
namespace Applications\Acl;
12
13
use Core\Entity\DraftableEntityInterface;
14
use Zend\Permissions\Acl\Assertion\AssertionInterface;
15
use Zend\Permissions\Acl\Acl;
16
use Zend\Permissions\Acl\Resource\ResourceInterface;
17
use Zend\Permissions\Acl\Role\RoleInterface;
18
use Applications\Entity\ApplicationInterface;
19
use Auth\Entity\UserInterface;
20
use Core\Entity\PermissionsInterface;
21
22
/**
23
 * Checks permission on attachments
24
 *
25
 * @author Mathias Gelhausen <[email protected]>
26
 * @author Carsten Bleek <[email protected]>
27
 * @author Miroslav Fedeleš <[email protected]>
28
 * @since 0.27 Checks, if application is a draft and only allow the associated user if so.
29
 * @since 0.4
30
 */
31
class ApplicationAccessAssertion implements AssertionInterface
32
{
33
    /**
34
     * Checks permissions based on resources' permissions.
35
     *
36
     * {@inheritDoc}
37
     *
38
     * @see \Zend\Permissions\Acl\Assertion\AssertionInterface::assert()
39
     */
40
    public function assert(
41
        Acl $acl,
42
        RoleInterface $role = null,
43
        ResourceInterface $resource = null,
44
        $privilege = null
45
    ) {
46
        if (!$role instanceof UserInterface || !$resource instanceof ApplicationInterface) {
47
            return false;
48
        }
49
50
        /* @var $resource ApplicationInterface|DraftableEntityInterface */
51
52
        /* If application is a draft, only the associated user may view and edit. */
53
        if ($resource->isDraft()) {
54
            return $role === $resource->getUser();
55
        }
56
57
        $permissions = $resource->getPermissions();
58
        
59
        if (ApplicationInterface::PERMISSION_SUBSEQUENT_ATTACHMENT_UPLOAD == $privilege) {
60
            // only applicant is allowed to upload subsequent attachments
61
            return $permissions->isAssigned($role) && $permissions->isGranted($role, PermissionsInterface::PERMISSION_VIEW);
62
        }
63
        
64
        $permission = 'read' == $privilege ? PermissionsInterface::PERMISSION_VIEW : PermissionsInterface::PERMISSION_CHANGE;
65
        return $permissions->isGranted($role, $permission);
66
    }
67
}
68