aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/restapi/UserPermissions.php
blob: dcf16019f01d793ef5fced2999537a42835c4250 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace RESTAPI;
use DBManager, PDO;

/**
 * REST API routing permissions
 *
 * @author     Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license    GPL 2 or later
 * @since      Stud.IP 2.6
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 */
class UserPermissions
{
    /**
     * Create a permission object (for a certain user).
     * Permissions object will be cached for each user.
     *
     * @param mixed $user_id Id of user (optional, defaults to global)
     * @return UserPermissions Returns permissions object
     */
    public static function get($user_id = null)
    {
        $user_id = $user_id ?: $GLOBALS['user']->id;

        static $cache = [];
        if (!isset($cache[$user_id])) {
            $cache[$user_id] = new self($user_id);
        }

        return $cache[$user_id];
    }

    private $user_id;
    private $permissions = [];

    /**
     * Creates the actual permission object (for a certain user).
     *
     * @param mixed $user_id Id of user (optional, defaults to global)
     */
    private function __construct($user_id = null)
    {
        $this->user_id = $user_id;

        // Init with global permissions
        $this->loadPermissions();
    }

    /**
     * Defines whether access is allowed for the current user to the
     * passed route via the passed method.
     *
     * @param String $user_id Id of the user
     * @param mixed  $granted  Granted state (PHP'ish boolean)
     * @return UserPermissions Returns instance of self to allow chaining
     */
    public function set($user_id, $granted = true)
    {
        $this->permissions[$user_id] = (bool)$granted;

        return $this;
    }

    /**
     * Loads permissions for passed user.
     *
     * @return UserPermissions Returns instance of self to allow chaining
     */
    protected function loadPermissions()
    {
        $query = "SELECT consumer_id, granted
                  FROM api_user_permissions
                  WHERE user_id = :user_id";
        $statement = DBManager::get()->prepare($query);
        $statement->bindValue(':user_id', $this->user_id);
        $statement->execute();
        $permissions = $statement->fetchAll(PDO::FETCH_ASSOC);

        // Init with global permissions
        foreach ($permissions as $permission) {
            extract($permission);

            $this->set($permission['consumer_id'], $permission['granted']);
        }

        return $this;
    }

    /**
     * Checks if access to consumer is allowed for the current user.
     *
     * @param String $consumer_id  Id of the consumer
     * @return bool Indicates whether access is allowed
     */
    public function check($consumer_id)
    {
        return isset($this->permissions[$consumer_id])
            && $this->permissions[$consumer_id];
    }

    /**
     * Stores the set permissions.
     *
     * @return bool Returns true if permissions were stored successfully
     */
    public function store()
    {
        $result = true;

        $query = "INSERT INTO api_user_permissions (user_id, consumer_id, granted, mkdate, chdate)
                  VALUES (:user_id, :consumer_id, :granted, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())
                  ON DUPLICATE KEY UPDATE granted = VALUES(granted),
                                          chdate = UNIX_TIMESTAMP()";
        $statement = DBManager::get()->prepare($query);
        $statement->bindValue(':user_id', $this->user_id);

        foreach ($this->permissions as $consumer_id => $granted) {
            $statement->bindValue(':consumer_id', $consumer_id);
            $statement->bindValue(':granted', (int) !empty($granted));

            $result = $result && $statement->execute();
        }

        return $result;
    }

    /**
     * Get a list of all consumer the user has granted acces to.
     *
     * @return array List of consumer objects
     */
    public function getConsumers()
    {
        $result = [];
        foreach ($this->permissions as $consumer_id => $granted) {
            if (!$granted) {
                continue;
            }
            $result[$consumer_id] = Consumer\Base::find($consumer_id);
        }
        return $result;
    }
}