summaryrefslogtreecommitdiff
path: root/apps/um/models/Auth.class.php
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2012-01-07 08:21:00 -0800
committerLuke Shumaker <LukeShu@sbcglobal.net>2012-01-07 10:22:21 -0800
commit83e460cdc3fc09867a3adb48c3d0894579dd3050 (patch)
tree0771bd935b30971bf2c244b6f158ed7496b644e5 /apps/um/models/Auth.class.php
parent3d64793a1ee45857856be1cd71c3a0a040a3e869 (diff)
Refactor to separate the framework from the app; drop message stuff, this app is just user management. Add a json view for individual users
Diffstat (limited to 'apps/um/models/Auth.class.php')
-rw-r--r--apps/um/models/Auth.class.php157
1 files changed, 157 insertions, 0 deletions
diff --git a/apps/um/models/Auth.class.php b/apps/um/models/Auth.class.php
new file mode 100644
index 0000000..39f627e
--- /dev/null
+++ b/apps/um/models/Auth.class.php
@@ -0,0 +1,157 @@
+<?php
+require_once('Model.class.php');
+require_once('Login.class.php');
+require_once('Database.class.php');
+
+require_once('Group.class.php');
+require_once('User.class.php');
+
+class Auth extends Model {
+ /**********************************************************************\
+ * Multiton stuff *
+ \**********************************************************************/
+ private static $users = array();
+ public static function getInstance($uid) {
+ if (!isset(self::$users[$uid])) {
+ $type = Database::getInstance()->getStatus($uid);
+ switch ($type) {
+ case 0: // unactivated user
+ case 1: // user
+ case 2: $obj = new User($uid); // admin
+ case 3: $obj = new Group($uid);
+ case 4: $obj = new Auth($uid); // deleted
+ }
+ self::$users[$uid] = $obj;
+ }
+ return self::$users[$uid];
+ }
+
+ /**********************************************************************\
+ * Static stuff *
+ \**********************************************************************/
+ public static function isNameLegal($name) {
+ // Current rules:
+ // * Not in "$illegal_names"
+ // * Does not contain '.'
+ // * Fewer than 256 characters
+ $illegal_names = array('', 'new', 'index', 'all');
+ return true
+ && (!in_array($name, $illegal_names))
+ && (strpos($name,'.')===false)
+ && (strpos($name,'!')===false)
+ && (strlen($name)<256);
+ }
+
+ /**********************************************************************\
+ * Class stuff *
+ \**********************************************************************/
+ protected $uid = false;
+ public function __construct($uid) {
+ parent::__construct();
+ $this->uid = $uid;
+ }
+ public function getUID() {
+ return $this->uid;
+ }
+
+ /**********************************************************************\
+ * The 'auth' table. *
+ \**********************************************************************/
+
+ // Row Type ////////////////////////////////////////////////////////////
+ /**
+ * @return 0=unverified 1=user 2=admin 3=group 4=deleted
+ */
+ protected function getType() {
+ $type = $this->db->getStatus($this->uid);
+ return $type;
+ }
+ protected function setType($type) {
+ $logged_in_uid = Login::isLoggedIn();
+ $logged_in_obj = Auth::getInstance($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));
+ }
+ public function delete() {
+ $this->setType(4);
+ }
+
+ // Permissions /////////////////////////////////////////////////////////
+ public function canRead() {
+ $logged_in_uid = Login::isLoggedIn();
+ $is_me = ($logged_in_uid === $this->uid);
+
+ $logged_in_obj = Auth::getInstance($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::getInstance($logged_in_uid);
+ $is_admin = $logged_in_obj->isAdmin();
+
+ return ($is_me || $is_admin);
+ }
+
+ // [user|group]name ////////////////////////////////////////////////////
+ public function getName() {
+ if ($this->db===null) {
+ return false;
+ } else {
+ return $this->db->getUsername($this->uid);
+ }
+ }
+ public function setName($new_name) {
+ if (!$this->canEdit()) return false;
+ if (!self::isNameLegal($new_name)) 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);
+ }
+}