5 * @author Sebastian Skejø
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");
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
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);
33 * Tries to login with the given $username and $password
34 * @param AOOSModel $data A model containing data to login
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");
49 "USERNAME" => $username,
50 "PASSWORD" => $password,
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);
58 $m = $this->_storageObj
->selectModel("*", $where);
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));
71 * Creates a user in the database
72 * @param AOOSModel $data The model containing all the data for the user to be created
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);
83 // We only want to insert these fields
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)) {
105 * Activates a given user
106 * @param AOOSModel $data A model containing username and password
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");
115 "USERNAME" => "'".$username."'",
116 "PASSWORD" => "'".$password."'",
119 $fields = array_keys($where);
120 $data->setColumnIndex($fields);
123 if ($this->_storageObj
->numRows($where) == 0) {
124 throw new AOOSException($this->core(), $this->tr("user_not_activatable", "User"), "", true, 1);
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
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);
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
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);
164 $where = array("USERNAME" => $username);
165 return $this->_storageObj
->updateFromModel($data, $where);