Overview

Namespaces

  • Sleepy
  • Module
    • Authentication
    • CSV
    • DB
    • FormBuilder
    • FSDB
    • IP2Country
    • Mailer
    • MobiDetect
    • Navigation
    • StaticCache
  • PHP

Classes

  • Sleepy\Debug
  • Sleepy\Hook
  • Sleepy\Router
  • Sleepy\SM
  • Sleepy\Template

Exceptions

  • Sleepy\RouteNotFound
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: namespace Module\Authentication;
  3: 
  4: require_once(DIRBASE . '/modules/enabled/db/class.record.php');
  5: 
  6: /**
  7:  * Example of using the User module
  8:  *
  9:  * <code>
 10:  *  if (class_exists('User')) {
 11:  *      // Try to login
 12:  *      try {
 13:  *          $u = new \Module\Authentication\User();
 14:  *
 15:  *          // check if the admin user exists
 16:  *          $uid = $u->authenticate('developer@envivent.com', 'test');
 17:  *
 18:  *          echo "Authentication successful. User ID is ", $uid;
 19:  *      } catch (\Exception $e) {
 20:  *          // Admin uses doesn't exist. Let's create it.
 21:  *          $u->columns['role_id'] = 1;
 22:  *          $u->columns['email'] = 'developer@envivent.com';
 23:  *          $u->columns['password'] = $u->saltPassword('test');
 24:  *          $u->save();
 25:  *      }
 26:  *  }
 27:  * </code>
 28:  */
 29: class User extends \Module\DB\Record {
 30:     /**
 31:      * The name of the users table
 32:      * @var string
 33:      */
 34:     public $table = 'users';
 35: 
 36:     /**
 37:      * The meta data for the table, e.g. fields and field data
 38:      * @var mixed
 39:      */
 40:     public $metadata;
 41: 
 42:     /**
 43:      * The users Role
 44:      * @var Role
 45:      */
 46:     private $role;
 47: 
 48: 
 49:     /**
 50:      * The salt for password encryption
 51:      * @var string
 52:      */
 53:     private $salt = 'o_PXO=1-BCTkq>|>*}KmkM8CA-!x|J6Y/UyDJC#ph(*A6me>CJ1Uu8E7gye|Vek[';
 54: 
 55:     /**
 56:      * Authenticates a user
 57:      * @param  string $email The username
 58:      * @param  string $pass  The password
 59:      * @return int           The user_id
 60:      */
 61:     public function authenticate($email, $pass) {
 62:         $pass = crypt($pass, $this->salt);
 63:         $query = $this->db->prepare("SELECT * FROM users WHERE email=:email AND password=:pass");
 64:         $query->execute(array(
 65:             ':email' => $email,
 66:             ':pass' => $pass
 67:         ));
 68:         $query->setFetchMode(\PDO::FETCH_ASSOC);
 69: 
 70:         if ($row = $query->fetch()) {
 71:             $this->load($row['id']);
 72:             return $row['id'];
 73:         } else {
 74:             throw new \Exception("Invalid user or password.");
 75:         }
 76:     }
 77: 
 78:     /**
 79:      * Gets the permission for their role
 80:      * @param  string $key What role do you want to query?
 81:      * @return string the value associated with the permission e.g. True, False, etc.
 82:      */
 83:     public function hasPermission($key) {
 84:         if (!isset($this->role)) {
 85:             throw \Exception('Role has not been set.');
 86:         }
 87: 
 88:         return $this->role->getPermission($key);
 89:     }
 90: 
 91:     /**
 92:      * Gets the data associated with the user
 93:      * @param  string $key The data you want to lookup
 94:      * @return mixed       The data associated with the key
 95:      */
 96:     public function getUserData($key) {
 97:         if (isset($this->columns[$key])) {
 98:             return $this->columns[$key];
 99:         } else {
100:             if (isset($this->metadata[$key])) {
101:                 return $this->metadata[$key];
102:             }
103:         }
104:     }
105: 
106:     /**
107:      * Loads the user
108:      * @param  integer $id The user ID
109:      * @return void
110:      */
111:     public function load($id=0) {
112:         parent::load($id);
113: 
114:         // Load usermeta data
115:         $query = $this->db->query("SELECT * FROM usermeta where user_id={$this->columns['id']}");
116:         $query->setFetchMode(\PDO::FETCH_ASSOC);
117:         $query->execute();
118: 
119:         $metadata = $query->fetchAll();
120: 
121:         foreach ($metadata as $data) {
122:             $temp = new UserMeta($data['id']);
123:             $this->metadata[$temp->columns['key']] = $temp->columns['value'];
124:         }
125: 
126:         // Load the Role
127:         $this->role = new Role($this->columns['role_id']);
128:     }
129: 
130:     /**
131:      * Sets user data by a key
132:      * @param string $key   The key to store the data into
133:      * @param mixed $value  The data to store at the key
134:      */
135:     public function setUserData($key, $value) {
136:         if (isset($this->columns[$key])) {
137:             $this->columns[$key] = $value;
138:         } else {
139:             if (isset($this->metadata[$key])) {
140:                 $this->metadata[$key] = $value;
141:             } else {
142:                 $temp = new UserMeta();
143:                 $temp->columns['key'] = $key;
144:                 $temp->columns['value'] = $value;
145:                 $temp->save();
146:             }
147:         }
148:     }
149: 
150:     /**
151:      * Salts the password
152:      * @param  string $pass The password
153:      * @return string       The encrypted password
154:      */
155:     public function saltPassword($pass) {
156:         return crypt($pass, $this->salt);
157:     }
158: 
159:     /**
160:      * Saves the user
161:      * @return void
162:      */
163:     public function save() {
164:         $query = $this->db->prepare("SELECT * from users where email=:email");
165:         $query->setFetchMode(\PDO::FETCH_ASSOC);
166:         $query->execute(array(':email' => $this->columns['email']));
167: 
168:         if ($query->fetch()) {
169:             throw new \Exception('The user already exists.');
170:         } else {
171:             parent::save();
172:             $this->load($this->columns['id']);
173:         }
174:     }
175: 
176:     /**
177:      * Checks if a user is loaded
178:      * @return boolean True, if loaded
179:      */
180:     public function isLoaded() {
181:         if (isset($this->columns['id'])) {
182:             return true;
183:         }
184:     }
185: 
186:     /**
187:      * Checks if a user is logged in
188:      * @return boolean True, if logged in
189:      */
190:     public function isLoggedIn() {
191:         if (isset($_SESSION['uid'])) {
192:             return $_SESSION['uid'];
193:         } else {
194:             return false;
195:         }
196:     }
197: 
198:     /**
199:      * Checks if a user is an admin (role_id == 1)
200:      * @return boolean True, if role_id ==  1
201:      */
202:     public function isAdmin() {
203:         if (!$this->isLoggedIn()) {
204:             return false;
205:         }
206: 
207:         if (!$this->isLoaded()) {
208:             $this->load($uid);
209:         }
210: 
211:         if ($this->columns['role_id'] == 1) {
212:             return true;
213:         } else {
214:             return false;
215:         }
216:     }
217: 
218:     /**
219:      * Gets the name of the role of the user
220:      * @return string The role
221:      */
222:     public function getRole() {
223:         return $this->role->columns['name'];
224:     }
225: }
226: 
227: /**
228:  * The users meta data
229:  */
230: class UserMeta extends \Module\DB\Record {
231:     public $table = "usermeta";
232: }
233: 
234: /**
235:  * The users role
236:  */
237: class Role extends \Module\DB\Record {
238:     public $table = 'roles';
239:     private $_permissions;
240: 
241:     /**
242:      * The the permission for the role
243:      * @param  string $key The permission key
244:      * @return mixed       The role data, e.g. True/False, etc.
245:      */
246:     public function getPermission($key) {
247:         if (isset($this->_permissions[$key])) {
248:             return $this->_permissions[$key];
249:         } else {
250:             throw new \Exception("{$key} permission does not exist.");
251:         }
252:     }
253: 
254:     /**
255:      * Loads the role
256:      * @param  integer $id role_id
257:      * @return void
258:      */
259:     public function load($id=0) {
260:         parent::load($id);
261: 
262:         // Load permissions
263:         $query = $this->db->query("SELECT * FROM permissions where role_id={$this->columns['id']}");
264:         $query->setFetchMode(\PDO::FETCH_ASSOC);
265:         $query->execute();
266: 
267:         $permissions = $query->fetchAll();
268: 
269:         foreach ($permissions as $p) {
270:             $temp = new Permission($p['id']);
271:             $this->_permissions[$temp->columns['key']] = $temp->columns['value'];
272:         }
273:     }
274: 
275:     /**
276:      * Sets a permission for this role
277:      * @param string $key   The permission key
278:      * @param void
279:      */
280:     public function setPermission($key, $value) {
281:         if (isset($this->_permissions[$key])) {
282:             $this->_permissions[$key] = $value;
283:         } else {
284:             // Create a new permission
285:             $temp = new Permission();
286:             $temp->columns['key'] = $key;
287:             $temp->columns['value'] = $value;
288:             $temp->save();
289: 
290:             // Add it to the permissions array
291:             array_push($this->_permissions, $temp);
292:         }
293:     }
294: }
295: 
296: /**
297:  * A row in the permission table
298:  */
299: class Permission extends \Module\DB\Record {
300:     public $table = 'permissions';
301: }
sleepyMUSTACHE v.0.8 API documentation generated by ApiGen