summaryrefslogtreecommitdiffstats
path: root/src/Psecio/Gatekeeper/GroupCollection.php
blob: 90e8b074d8854217a19557fa53a8135c6cf7516f (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
<?php

namespace Psecio\Gatekeeper;

class GroupCollection extends \Psecio\Gatekeeper\Collection\Mysql
{
    /**
     * Find child groups by the parent group ID
     *
     * @param integer $groupId Group ID
     */
    public function findChildrenByGroupId($groupId)
    {
        $prefix = $this->getPrefix();
        $data = array('groupId' => $groupId);
        $sql = 'select g.* from '.$prefix.'groups g, '.$prefix.'group_parent gp'
            .' where g.id = gp.group_id'
            .' and gp.parent_id = :groupId';

        $results = $this->getDb()->fetch($sql, $data);

        foreach ($results as $result) {
            $group = new GroupModel($this->getDb(), $result);
            $this->add($group);
        }
    }

    /**
     * Find the groups that a permission belongs to
     *
     * @param integer $permId Permission ID
     */
    public function findGroupsByPermissionId($permId)
    {
        $prefix = $this->getPrefix();
        $data = array('permId' => $permId);
        $sql = 'select g.* from '.$prefix.'groups g, '.$prefix.'group_permission gp'
            .' where gp.permission_id = :permId'
            .' and gp.group_id = g.id';

        $results = $this->getDb()->fetch($sql, $data);

        foreach ($results as $result) {
            $group = new GroupModel($this->getDb(), $result);
            $this->add($group);
        }
    }
}