summaryrefslogtreecommitdiff
path: root/src/lib/Auth.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Auth.class.php')
-rw-r--r--src/lib/Auth.class.php105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/lib/Auth.class.php b/src/lib/Auth.class.php
new file mode 100644
index 0000000..4c2a9c6
--- /dev/null
+++ b/src/lib/Auth.class.php
@@ -0,0 +1,105 @@
+<?php
+require_once('MessageManager.class.php');
+
+class Auth {
+ protected $mm = null;
+ protected $uid = false;
+ public function __construct($uid) {
+ global $mm;
+ $this->mm = $mm;
+ $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->mm->getStatus($this->uid);
+ return $type;
+ }
+ protected function setType($type) {
+ return $this->mm->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 = $this->mm->isLoggedIn();
+ $is_me = ($logged_in_uid === $this->uid);
+
+ $logged_in_obj = $this->mm->getAuthObj($logged_in_uid);
+ $is_user = $logged_in_obj->isUser();
+
+ return ($is_me || $is_user);
+ }
+ public function canEdit() {
+ $logged_in_uid = $this->mm->isLoggedIn();
+ $is_me = ($logged_in_uid === $this->uid);
+
+ $logged_in_obj = $this->mm->getAuthObj($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->mm->getUsername($this->uid);
+ }
+ public function setName($new_name) {
+ if (!$this->canEdit()) return false;
+ return $this->mm->setUsername($this->uid, $new_name);
+ }
+
+ /**********************************************************************\
+ * The 'users' table. *
+ \**********************************************************************/
+
+ public function getConf($setting) {
+ if (!$this->canRead()) return false;
+ return $this->mm->getUserConf($this->uid, $setting);
+ }
+ public function setConf($setting, $value) {
+ if (!$this->canEdit()) return false;
+ return $this->mm->setUserConf($this->uid, $setting, $value);
+ }
+ public function getConfArray($setting) {
+ $string = $this->getConf($setting);
+ return $this->mm->valueToArray($string);
+ }
+ public function setConfArray($setting, $list) {
+ $string = $this->mm->arrayToValue($list);
+ return $this->setConf($setting, $string);
+ }
+}