summaryrefslogtreecommitdiff
path: root/src/models/Auth.class.php
blob: f2c912025570ecfd4bef77e6195a13366c39c614 (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
<?php
require_once('MessageManager.class.php');
require_once('Login.class.php');
require_once('Group.class.php');
require_once('User.class.php');

class Auth {
	static $users = array();
	public static function getObj($uid) {
		if (!isset(self::$users[$uid])) {
			global $mm;
			$is_group = ($mm->database()->getStatus($uid)===3);
			if ($is_group) {
				require_once('Group.class.php');
				$obj = new Group($uid);
			} else {
				require_once('User.class.php');
				$obj = new User($uid);
			}
			self::$users[$uid] = $obj;
		}
		return self::$users[$uid];
	}

	protected $db = null;
	protected $uid = false;
	public function __construct($uid) {
		global $mm;
		$this->db = $mm->database();
		$this->uid = $uid;
	}
	public function getUID() {
		return $this->uid;
	}
	
	/**********************************************************************\
	 * The 'auth' table.                                                  *
	\**********************************************************************/
	
	// Row Type ////////////////////////////////////////////////////////////
	/**
	 * @return 0=unverified 1=user 2=admin 3=group
	 */
	protected function getType() {
		$type = $this->db->getStatus($this->uid);
		return $type;
	}
	protected function setType($type) {
		$logged_in_uid = Login::isLoggedIn();
		$logged_in_obj = Auth::getObj($logged_in_uid);
		$is_admin = $logged_in_obj->isAdmin();
		if (!$is_admin) return false;
		return $this->db->setStatus($this->uid, $type);
	}
	public function isUser() {
		$type = $this->getType();
		return ($type===1) || ($type===2);
	}
	public function isAdmin() {
		$type = $this->getType();
		return ($type===2);
	}
	public function isGroup() {
		$type = $this->getType();
		return ($type===3);
	}
	public function setUser($is_user) {
		$is_user = ($is_user?true:false);
		if ($this->isUser() != $is_user) {
			$this->setType($is_user?1:0);
		}
	}
	public function setAdmin($is_admin) {
		$is_admin = ($is_admin?true:false);
		$is_user = $this->isUser();
		$this->setType($is_admin?2:($is_user?1:0));
	}
	
	// Permissions /////////////////////////////////////////////////////////
	public function canRead() {
		$logged_in_uid = Login::isLoggedIn();
		$is_me = ($logged_in_uid === $this->uid);
		
		$logged_in_obj = Auth::getObj($logged_in_uid);
		$is_user = $logged_in_obj->isUser();
		
		return ($is_me || $is_user);
	}
	public function canEdit() {
		$logged_in_uid = Login::isLoggedIn();
		$is_me = ($logged_in_uid === $this->uid);
		
		$logged_in_obj = Auth::getObj($logged_in_uid);
		$is_admin = $logged_in_obj->isAdmin();
		
		return ($is_me || $is_admin);
	}

	// [user|group]name ////////////////////////////////////////////////////
	public function getName() {
		if (!$this->canRead()) return false;
		return $this->db->getUsername($this->uid);
	}
	public function setName($new_name) {
		if (!$this->canEdit()) return false;
		return $this->db->setUsername($this->uid, $new_name);
	}
	
	/**********************************************************************\
	 * The 'users' table.                                                 *
	\**********************************************************************/
	
	public function getConf($setting) {
		if (!$this->canRead()) return false;
		return $this->db->getUserConf($this->uid, $setting);
	}
	public function setConf($setting, $value) {
		if (!$this->canEdit()) return false;
		return $this->db->setUserConf($this->uid, $setting, $value);
	}
	public function getConfArray($setting) {
		$string = $this->getConf($setting);
		return $this->db->valueToArray($string);
	}
	public function setConfArray($setting, $list) {
		$string = $this->db->arrayToValue($list);
		return $this->setConf($setting, $string);
	}
}