Update of show() and places that uses AOOSModel::getColumn
[AOOS.git] / modules / User / UserHandler.php
blob1c2f4a5c2f2bb7e6d97cd493dbe4980f318bca98
1 <?php
3 /**
4 * Handles user data
5 * @author Sebastian Skejø
6 */
8 class UserHandler extends AOOSModule
10 private $_storageObj = null;
12 public function __construct($core) {
13 parent::__construct($core);
15 $this->_storageObj = $this->core()->newStorageDevice();
16 $this->_storageObj->setTable("User");
18 /**
19 * Returns a model with the fields given in $fields, selected by $where and ordered by $sort
20 * @param array $fields Fields to be returned
21 * @param where-clause $where A valid where-clause
22 * @param field $order The field the data is ordered by
23 * @param ASC|DESC $sort Determines if data should be ordered
24 * @return AOOSModel
26 public function getUserList($fields, $where = null, $limit = null, $order = "username", $sort = "ASC") {
27 $this->_storageObj->setSort($sort);
28 $model = $this->_storageObj->selectModel($fields, $where, $limit, $order);
29 return true;
32 /**
33 * Tries to login with the given $username and $password
34 * @param AOOSModel $data A model containing data to login
35 * @return bool
37 public function login(AOOSModel $data) {
38 // We have to do it in this order since passwords don't need quotes until we it is encrypted
39 // XXX This part is quite ugly - should be take care of in StorageDevice
40 $password = $data->getColumn("PASSWORD", true);
41 $data->setQuote(true);
42 $username = $data->getColumn("USERNAME", true);
44 $password = hash("sha256", $password); // XXX Need to check if this is supported on the server!
45 $password = "'".$password."'";
46 $u = $this->core()->getModule("User");
48 $where = array(
49 "USERNAME" => $username,
50 "PASSWORD" => $password,
51 "ACTIVATED" => 1
53 if (!($m = $this->_storageObj->selectModel("*", $where))) {
54 throw new AOOSException($this->core(), $this->tr("login_failed", "User"), $this->tr("check_user_pass_active", "User"), true, 1);
55 return false;
58 // Set the userdata
59 $u->setUsername($m->getColumn("USERNAME", true));
60 $u->setPassword($m->getColumn("PASSWORD", true));
61 // $u->setStatus(1); // 1 == online XXX Could be neat
62 $u->setLoggedIn(true);
63 $u->setEmail($m->getColumn("EMAIL", true));
64 $u->setGroups($m->getColumn("GROUPS", true));
65 $u->setLevel($m->getColumn("LEVEL", true));
66 return true;
69 /**
70 * Creates a user in the database
71 * @param AOOSModel $data The model containing all the data for the user to be created
72 * @return bool
74 public function createUser($data) {
75 $username = $data->getColumn("USERNAME");
76 $where = array("USERNAME" => $username);
77 if ($this->_storageObj->numRows($where) != 0) {
78 throw new AOOSException($this->core(), $this->tr("username_taken", "User"), "", true, 1);
79 return false;
82 // We only want to insert these fields
83 $fields = array(
84 "USERNAME",
85 "PASSWORD",
86 "EMAIL"
89 // Encryption of password
90 $uPass = $data->getColumn("PASSWORD");
91 $pass = hash("sha256", $uPass[0]);
92 $data->setData($pass, -1, "PASSWORD");
94 $data->setColumnIndex($fields);
95 return $this->_storageObj->insertModel($data);
98 /**
99 * Deletes the user, $username
100 * @param string $username The user
101 * @return bool
103 public function deleteUser($username) {
104 // Only admins have rights to do this
105 if (!$this->core()->getModule("user")->checkLevel("admin")) {
106 throw new AOOSException($this->core(), $this->tr("access_denied", "User"), "", true, 1);
107 return false;
110 $d = array("USERNAME" => $username);
111 return $this->_storageObj->deleteFromArray($d);
115 * Updates the data for the given user
116 * @param string $username The username of the user which data we update
117 * @param AOOSModel $data All the new data, saved in an AOOSModel
118 * @return bool
120 public function updateUser($username, $data) {
121 $u = $this->core()->getModule("user");
123 // Only admins or the user itself have rights to do this
124 if (!($u->checkLevel("admin") || $u->username() == $username)) {
125 throw new AOOSException($this->core(), $this->tr("access_denied", "User"), "", true, 1);
126 return false;
129 $where = array("USERNAME" => $username);
130 return $this->_storageObj->updateFromModel($data, $where);