summaryrefslogtreecommitdiffstats
path: root/src/Psecio/Gatekeeper/PermissionModel.php
blob: 474b176d7d5c2017e5ac328adc6393b25d9685c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

namespace Psecio\Gatekeeper;

class PermissionModel extends \Psecio\Gatekeeper\Model\Mysql
{
    /**
     * Database table name
     * @var string
     */
    protected $tableName = 'permissions';

    /**
     * Model properties
     * @var array
     */
    protected $properties = array(
        'name' => array(
            'description' => 'Group Name',
            'column' => 'name',
            'type' => 'varchar'
        ),
        'description' => array(
            'description' => 'Description',
            'column' => 'description',
            'type' => 'text'
        ),
        'id' => array(
            'description' => 'Group ID',
            'column' => 'id',
            'type' => 'integer'
        ),
        'created' => array(
            'description' => 'Date Created',
            'column' => 'created',
            'type' => 'datetime'
        ),
        'updated' => array(
            'description' => 'Date Updated',
            'column' => 'updated',
            'type' => 'datetime'
        ),
        'children' => array(
            'description' => 'Child Permissions',
            'type' => 'relation',
            'relation' => array(
                'model' => '\\Psecio\\Gatekeeper\\PermissionCollection',
                'method' => 'findChildrenByPermissionId',
                'local' => 'id'
            )
        )
    );

    /**
     * Add a permission as a child of the current instance
     *
     * @param integer|PermissionModel $permission Either permission ID or model instance
     * @return boolean Result of save operation
     */
    public function addChild($permission)
    {
        if ($this->id === null) {
            return false;
        }
        if ($permission instanceof PermissionModel) {
            $permission = $permission->id;
        }
        $childPermission = new PermissionParentModel(
            $this->getDb(),
            array('permission_id' => $permission, 'parent_id' => $this->id)
        );
        return $this->getDb()->save($childPermission);
    }

    /**
     * Remove a permission as a child of this instance
     *
     * @param integer|PermissionModel $permission Either permission ID or model instance
     * @return boolean Resultk of delete operation
     */
    public function removeChild($permission)
    {
        if ($this->id === null) {
            return false;
        }
        if ($permission instanceof PermissionModel) {
            $permission = $permission->id;
        }
        $childPermission = new PermissionParentModel($this->getDb());

        $childPermission = $this->getDb()->find(
            $childPermission,
            array('permission_id' => $permission, 'parent_id' => $this->id)
        );
        return $this->getDb()->delete($childPermission);
    }
}