xref: /plugin/bez/mdl/Model.php (revision 522c019c0bd8bcd6e522fddaa2cf94cce8e0780d)
1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5//ACL level defines
6define('BEZ_AUTH_NONE', 0);
7define('BEZ_AUTH_VIEWER', 2);
8define('BEZ_AUTH_USER', 5);
9define('BEZ_AUTH_LEADER', 10);
10define('BEZ_AUTH_ADMIN', 20);
11
12define('BEZ_PERMISSION_UNKNOWN', -1);
13define('BEZ_PERMISSION_NONE', 0);
14define('BEZ_PERMISSION_VIEW', 1);
15define('BEZ_PERMISSION_CHANGE', 2);
16define('BEZ_PERMISSION_DELETE', 3);
17
18
19class Model {
20    /** @var \helper_plugin_sqlite */
21	protected $sqlite;
22
23	/** @var \SQLite3 */
24	protected $db;
25
26    protected $level = BEZ_AUTH_NONE;
27
28    protected $dw_auth, $user_nick, $action, $conf;
29
30    /** @var ThreadFactory */
31    protected $threadFactory;
32
33    /** @var UserFactory  */
34    protected $userFactory;
35
36    /** @var LabelFactory */
37    protected $labelFactory;
38
39    /** @var Thread_commentFactory */
40    protected $thread_commentFactory;
41
42    /** @var TaskFactory */
43    protected $taskFactory;
44
45    /** @var Task_programFactory */
46    protected $task_programFactory;
47
48    /** @var Task_commentFactory */
49    protected $task_commentFactory;
50
51    /** @var Authentication_tokenFactory */
52    protected $authentication_tokenFactory;
53
54	public function __get($property) {
55        $models = array('userFactory', 'threadFactory', 'labelFactory', 'thread_commentFactory', 'taskFactory', 'task_programFactory', 'task_commentFactory', 'authentication_tokenFactory');
56		if (in_array($property, $models) ||
57            in_array($property, array('sqlite', 'db', 'acl', 'dw_auth', 'user_nick', 'action', 'conf'))) {
58			return $this->$property;
59		}
60	}
61
62	public function factory($table) {
63	    $prop = $table . 'Factory';
64
65	    return $this->$prop;
66    }
67
68    protected function update_level($level) {
69        if ($level > $this->level) {
70            $this->level = $level;
71        }
72    }
73
74    public function get_level() {
75        return $this->level;
76    }
77
78	public function __construct($dw_auth, $user_nick, $action, $skip_acl=false) {
79        $this->dw_auth = $dw_auth;
80        $this->user_nick = $user_nick;
81		$this->action = $action;
82        $this->conf = $action->getGlobalConf();
83
84        $this->db_helper =  plugin_load('helper', 'bez_db');
85
86        $this->sqlite = $this->db_helper->getDB();
87        $this->db = $this->sqlite->getAdapter()->getDb();
88        $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
89
90        $this->userFactory = new UserFactory($this);
91
92        $this->threadFactory = new ThreadFactory($this);
93
94        $this->labelFactory = new LabelFactory($this);
95
96        $this->thread_commentFactory = new Thread_commentFactory($this);
97
98        $this->taskFactory = new TaskFactory($this);
99
100        $this->task_programFactory = new Task_programFactory($this);
101
102        $this->task_commentFactory = new Task_commentFactory($this);
103
104        $this->authentication_tokenFactory = new Authentication_tokenFactory($this);
105
106        if ($skip_acl) {
107            $this->update_level(BEZ_AUTH_ADMIN);
108        } else {
109            $userd = $this->dw_auth->getUserData($this->user_nick);
110            if ($userd !== false && is_array($userd['grps'])) {
111                $grps = $userd['grps'];
112                if (in_array('admin', $grps ) || in_array('bez_admin', $grps )) {
113                    $this->update_level(BEZ_AUTH_ADMIN);
114                } elseif (in_array('bez_leader', $grps )) {
115                    $this->update_level(BEZ_AUTH_LEADER);
116                } else {
117                    $this->update_level(BEZ_AUTH_USER);
118                }
119            } elseif (isset($_GET['t'])) {
120                $page_id = $this->action->id();
121
122                $user_tok = trim($_GET['t']);
123                if ($this->authentication_tokenFactory->get_token($page_id) == $user_tok) {
124                    $this->update_level(BEZ_AUTH_VIEWER);
125                }
126            }
127        }
128    }
129}
130