1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5class Acl {
6
7    protected $acl = array();
8
9    protected $user_level;
10
11    public function __construct($user_level, $columns) {
12        $this->user_level = $user_level;
13
14        if ($this->user_level >= BEZ_AUTH_ADMIN) {
15            $this->acl = array_fill_keys($columns, BEZ_PERMISSION_DELETE);
16        } elseif ($this->user_level >= BEZ_AUTH_VIEWER) {
17            $this->acl = array_fill_keys($columns, BEZ_PERMISSION_VIEW);
18        } else {
19            $this->acl = array_fill_keys($columns, BEZ_PERMISSION_NONE);
20        }
21    }
22
23    public function grant($columns, $perm) {
24        if (!is_array($columns)) $columns = array($columns);
25
26        foreach($columns as $column) {
27            if (!array_key_exists($column, $this->acl)) {
28                throw new \Exception("column: $column not exists in table");
29            }
30
31            if ($this->acl[$column] < $perm) {
32                $this->acl[$column] = $perm;
33            }
34        }
35    }
36
37    public function revoke($columns, $level) {
38        if (!is_array($columns)) $columns = array($columns);
39
40        foreach ($columns as $column) {
41            if ($this->user_level <= $level) {
42                $this->acl[$column] = BEZ_PERMISSION_NONE;
43            }
44        }
45    }
46
47    public function acl_of($col) {
48        return $this->acl[$col];
49    }
50
51    public function get_list() {
52        return $this->acl;
53    }
54
55    public function add_column($column, $perm=BEZ_PERMISSION_NONE) {
56        if (isset($this->acl[$column])) {
57            throw new \Exception('column already exists');
58        }
59
60        $this->acl[$column] = $perm;
61    }
62}
63