Cleanup + AOOSCore::register + commit Form module
[AOOS.git] / modules / User / User.php
blobcb88ede63f908343e7b07d4d7c05d48cebc988f8
1 <?php
3 /**
4 * User-module
5 */
7 class User extends AOOSModule {
8 static public function dependencies() {
9 return array("Paginator", "Form", "Reciever");
12 static public function files() {
13 return array(
14 "UserView" => "UserView.php"
18 /**
19 * Model m1 contains information on the current user. If the user isn't logged in, the model is empty(maybe some
20 * default values later on). When the user logs in, data is fetched from the database and the model contains a
21 * single row with all of the user's information.
22 * Model m1 is also used when a new user is created. When that happens, data is inserted into a new row in the model
23 * and save is called on the model.
24 * Model m2 contains the uid and the username of all users in the database. This ensures that usernames can be
25 * pulled quickly when you have the uid of the user.
27 public function dataModelDefinition() {
28 // Current user model
29 $m1 = new AOOSModel($this->core());
30 $m1->setSource("mysql");
31 $m1->setTable("User");
32 $m1->setColumnIndex(array("uid", "username", "password", "email", "signature", /*"avatar", */"active", "activationcode", "joined", "status", "groups", "level"));
34 $m1->setProperties("uid", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_PRIMARY_KEY|AOOSMODEL_FLAG_GUI_PRIVATE);
36 $m1->setProperties("username", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_ESCAPE|AOOSMODEL_PROP_NOHTML|AOOSMODEL_PROP_STRIP);
37 $m1->addConstraint("username", "Match", "/[a-zA-Z0-9-_]+/");
38 $m1->addConstraint("username", "Length", 50);
40 $m1->setProperties("password", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_HASH|AOOSMODEL_PROP_STRIP);
42 $m1->setProperties("email", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_ESCAPE|AOOSMODEL_PROP_NOHTML|AOOSMODEL_PROP_STRIP);
43 $m1->addConstraint("email", "Match", "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/"); // Match email
45 $m1->setProperties("signature", AOOSMODEL_TYPE_TEXT, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_ESCAPE|AOOSMODEL_PROP_NOHTML);
46 $m1->addConstraint("signature", "Length", 256);
48 // XXX $m1->setProperties("avatar", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_ESCAPE);
50 $m1->setProperties("active", AOOSMODEL_TYPE_BOOLEAN, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
51 $m1->setDefaultValue("active", false);
53 $m1->setProperties("activationcode", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
55 $m1->setProperties("joined", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE, AOOSMODEL_PROP_TIME);
57 $m1->setProperties("status", AOOSMODEL_TYPE_INTEGER,AOOSMODEL_FLAG_GUI_PRIVATE);
58 $m1->setDefaultValue("status",0);// $this->getStatusValue("offline"));
60 $m1->setProperties("groups", AOOSMODEL_TYPE_TEXT, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
62 $m1->setProperties("level", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
64 $m1->appendRow();
65 $m1->getRow(0)->setFlag(0);
67 // All users. Only the most important fields are fetched here
68 $m2 = new AOOSModel($this->core());
69 $m2->setSource("mysql");
70 $m2->setTable("User");
71 $m2->setColumnIndex(array("uid", "username", "active", "activationcode"));
72 $m2->setProperties("uid", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_PRIMARY_KEY);
73 $m2->setProperties("username", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_UNIQUE, AOOSMODEL_PROP_ESCAPE|AOOSMODEL_PROP_NOHTML|AOOSMODEL_PROP_STRIP);
74 $m2->setProperties("active", AOOSMODEL_TYPE_BOOLEAN, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
75 $m2->setDefaultValue("active", false);
76 $m2->setProperties("activationcode", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
78 $a = array("USER" => $m1, "USERLIST" => $m2);
79 return $a;
82 public function show() {
83 $this->dataModel("USERLIST")->populate();
84 require_once("UserView.php");
85 $this->_view = new UserView($this);
86 $p = $this->core()->getModule("Paginator");
87 switch($p->getOption(0)) {
88 case("edit"):
89 return $this->_view->edit();
90 break;
91 case("add"):
92 return $this->_view->add();
93 break;
94 case("activate"):
95 $this->activateUser($p->getOption(1), $p->getOption(2));
96 return $this->_view->show();
97 break;
98 default:
99 return $this->_view->show();
100 break;
105 * Creates the user
107 public function createUser($info) {
108 if ($this->dataModel("USERLIST")->find(array("username" => $info->username))) {
109 return false;
111 $a = $info->toArray()+array("activationcode" => time());
112 try {
113 $this->dataModel("USER")->appendRow($a);
114 } catch (AOOSException $e) {
115 throw $e;
117 // Appending was succesful
118 $this->dataModel("USER")->save();
119 $this->dataModel("USERLIST")->populate();
120 $p = $this->core()->getModule("Paginator");
121 $uid = $this->dataModel("USERLIST")->find(array("username" => $info->username))->uid;
122 $url = $p->createURL("User", array("activate", $uid, $a["activationcode"]));
123 mail($info->email, "Activation code", "Activate at: ".$url);
124 return true;
127 public function updateUser($row) {
128 if (!$this->online()) {
129 return false;
131 foreach ($row->toArray() as $key => $val) {
132 $this->dataModel("USER")->getRow(0)->$key = $val;
134 $this->dataModel("USER")->save();
135 return true;
138 public function activateUser($uid, $code) {
139 $r = $this->dataModel("USERLIST")->find(array("uid" => $uid));
140 if ($r->activationcode == $code) {
141 $r->activationcode = 0;
142 $r->active = true;
143 $r->save();
145 return true;
148 public function login($row) {
149 $dm = $this->dataModel("USER");
150 $w = $row->toArray();
151 $w["active"] = true;
152 $dm->populate($w);
153 if ($dm->rows() == 0) {
154 WidgetInterface::addError("login_failed");
155 return false;
157 $dm->getRow(0)->status = 1;//$this->core()->getSetting("online", __class__);
158 return true;
161 /* ---- Convinience functions ---- */
162 public function status() {
163 if ($this->dataModel("USER")->rows() > 0) {
164 return $this->dataModel("USER")->getRow(0)->status;
166 return 0;
169 public function getStatusValue($string) {
170 return $this->core()->getSetting("status_".$string, __class__);
173 public function online() {
174 return $this->status() == 1;//$this->getStatusValue("online");
177 public function level() {
178 if (!$this->online()) {
179 return false;
181 return $this->dataModel("USER")->getRow(0)->level;
184 public function groups() {
185 if (!$this->online()) {
186 return false;
188 return $this->dataModel("USER")->getRow(0)->groups;
191 public function inGroup($group) {
192 if (!$this->online()) {
193 return false;
195 return in_array($group, $this->groups());
198 public function userInfo() {
199 return $this->dataModel("USER")->getRow(0);