diff options
author | Luke Shumaker <LukeShu@sbcglobal.net> | 2011-09-05 01:22:27 -0400 |
---|---|---|
committer | Luke Shumaker <LukeShu@sbcglobal.net> | 2011-09-05 01:22:27 -0400 |
commit | 570901ba4e526be37afd0cbbe018cb0500a7cda1 (patch) | |
tree | 81f13e66cfd795141cac216fd2af610e877a1a31 /src/models | |
parent | ad4a7ff9159c2c64cea98d7189f46fa7d6174fc2 (diff) |
Refactor a bit
* move a lot of stuff out of MessageManager
* move models from lib to models
Diffstat (limited to 'src/models')
-rw-r--r-- | src/models/Auth.class.php | 130 | ||||
-rw-r--r-- | src/models/ContactMethod.class.php | 30 | ||||
-rw-r--r-- | src/models/Group.class.php | 23 | ||||
-rw-r--r-- | src/models/User.class.php | 25 |
4 files changed, 208 insertions, 0 deletions
diff --git a/src/models/Auth.class.php b/src/models/Auth.class.php new file mode 100644 index 0000000..3aba0f3 --- /dev/null +++ b/src/models/Auth.class.php @@ -0,0 +1,130 @@ +<?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 = $this->db->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); + } +} diff --git a/src/models/ContactMethod.class.php b/src/models/ContactMethod.class.php new file mode 100644 index 0000000..b01e7d3 --- /dev/null +++ b/src/models/ContactMethod.class.php @@ -0,0 +1,30 @@ +<?php + +global $CONTACT_METHODS; +if (!isset($CONTACT_METHODS)) { + $CONTACT_METHODS = array(); +} + +class ContactMethod { + public $verb_slug = ''; // sms + public $addr_slug = ''; // phone + public $verb_text = ''; // text message + public $addr_text = ''; // phone number + + public $handler = null; + + public function __construct($verb_slug, $addr_slug, + $verb_text, $addr_text) + { + $this->verb_slug = $verb_slug; + $this->addr_slug = $addr_slug; + $this->verb_text = $verb_text; + $this->addr_text = $addr_text; + + global $CONTACT_METHODS; + $CONTACT_METHODS[$verb_slug] = $this; + } + public function setHandler($handler) { + $this->handler = $handler; + } +} diff --git a/src/models/Group.class.php b/src/models/Group.class.php new file mode 100644 index 0000000..f981a4f --- /dev/null +++ b/src/models/Group.class.php @@ -0,0 +1,23 @@ +<?php +require_once('Auth.class.php'); + +class Group extends Auth { + public function __construct($uid) { + parent::__construct($uid); + } + public function getUID() { + return $this->uid; + } + + /**********************************************************************\ + * The 'auth' table. * + \**********************************************************************/ + + /**********************************************************************\ + * The 'users' table. * + \**********************************************************************/ + + public function getMembers() { + return $this->db->getUsersInGroup($this->getName()); + } +} diff --git a/src/models/User.class.php b/src/models/User.class.php new file mode 100644 index 0000000..b6dbede --- /dev/null +++ b/src/models/User.class.php @@ -0,0 +1,25 @@ +<?php +require_once('Auth.class.php'); + +class User extends Auth { + public function __construct($uid) { + parent::__construct($uid); + } + public function getUID() { + return $this->uid; + } + + /**********************************************************************\ + * The 'auth' table. * + \**********************************************************************/ + + public function setPassword($password) { + if (!$this->canEdit()) return false; + return $this->db->setPassword($this->uid, $password); + } + + /**********************************************************************\ + * The 'users' table. * + \**********************************************************************/ + +} |