DOCUMENTATION+TODO added.
[AOOS.git] / modules / User / UserHandler.php
blobc2292434a9ee89f42f77f03c649d58b8984f7548
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 ($this->_storageObj->numRows($where) == 0) {
54 throw new AOOSException($this->core(), $this->tr("login_failed", "User"), $this->tr("check_user_pass_active", "User"), true, 1);
55 return false;
58 $m = $this->_storageObj->selectModel("*", $where);
59 // Set the userdata
60 $u->setUsername($m->getColumn("USERNAME", true));
61 $u->setPassword($m->getColumn("PASSWORD", true));
62 // $u->setStatus(1); // 1 == online XXX Could be neat
63 $u->setLoggedIn(true);
64 $u->setEmail($m->getColumn("EMAIL", true));
65 $u->setGroups($m->getColumn("GROUPS", true));
66 $u->setLevel($m->getColumn("LEVEL", true));
67 return true;
70 /**
71 * Creates a user in the database
72 * @param AOOSModel $data The model containing all the data for the user to be created
73 * @return bool
75 public function createUser($data) {
76 $username = $data->getColumn("USERNAME", true);
77 $where = array("USERNAME" => "'".$username."'");
78 if ($this->_storageObj->numRows($where) != 0) {
79 throw new AOOSException($this->core(), $this->tr("username_taken", "User"), "", true, 1);
80 return false;
83 // We only want to insert these fields
84 $fields = array(
85 "USERNAME",
86 "PASSWORD",
87 "EMAIL"
90 // Encryption of password
91 $uPass = $data->getColumn("PASSWORD", true);
92 $pass = hash("sha256", $uPass);
93 $data->setData($pass, -1, "PASSWORD");
95 $data->setColumnIndex($fields);
96 $data->setQuote(true);
97 if ($this->_storageObj->insertModel($data)) {
98 // XXX Send email
99 return true;
101 return false;
105 * Activates a given user
106 * @param AOOSModel $data A model containing username and password
107 * @return bool
109 public function activateUser($data) {
110 $username = $data->getColumn("USERNAME", true);
111 $password = hash("sha256", $data->getColumn("PASSWORD", true));
112 $data->setData($password, -1, "PASSWORD");
114 $where = array(
115 "USERNAME" => "'".$username."'",
116 "PASSWORD" => "'".$password."'",
117 "ACTIVATED" => 0
119 $fields = array_keys($where);
120 $data->setColumnIndex($fields);
121 print_r($where);
123 if ($this->_storageObj->numRows($where) == 0) {
124 throw new AOOSException($this->core(), $this->tr("user_not_activatable", "User"), "", true, 1);
125 return false;
128 $data->setData(1, -1, "ACTIVATED");
129 $data->setQuote(true);
130 return $this->_storageObj->updateFromModel($data, $where);
134 * Deletes the user, $username
135 * @param string $username The user
136 * @return bool
138 public function deleteUser($username) {
139 // Only admins have rights to do this
140 if (!$this->core()->getModule("user")->checkLevel("admin")) {
141 throw new AOOSException($this->core(), $this->tr("access_denied", "User"), "", true, 1);
142 return false;
145 $d = array("USERNAME" => $username);
146 return $this->_storageObj->deleteFromArray($d);
150 * Updates the data for the given user
151 * @param string $username The username of the user which data we update
152 * @param AOOSModel $data All the new data, saved in an AOOSModel
153 * @return bool
155 public function updateUser($username, $data) {
156 $u = $this->core()->getModule("user");
158 // Only admins or the user itself have rights to do this
159 if (!($u->checkLevel("admin") || $u->username() == $username)) {
160 throw new AOOSException($this->core(), $this->tr("access_denied", "User"), "", true, 1);
161 return false;
164 $where = array("USERNAME" => $username);
165 return $this->_storageObj->updateFromModel($data, $where);