7 class User
extends AOOSModule
{
8 static public function dependencies() {
9 return array("Paginator", "Form", "Reciever");
12 static public function files() {
14 "UserView" => "UserView.php"
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() {
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
);
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);
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)) {
89 return $this->_view
->edit();
92 return $this->_view
->add();
95 $this->activateUser($p->getOption(1), $p->getOption(2));
96 return $this->_view
->show();
99 return $this->_view
->show();
107 public function createUser($info) {
108 if ($this->dataModel("USERLIST")->find(array("username" => $info->username
))) {
111 $a = $info->toArray()+
array("activationcode" => time());
113 $this->dataModel("USER")->appendRow($a);
114 } catch (AOOSException
$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);
127 public function updateUser($row) {
128 if (!$this->online()) {
131 foreach ($row->toArray() as $key => $val) {
132 $this->dataModel("USER")->getRow(0)->$key = $val;
134 $this->dataModel("USER")->save();
138 public function activateUser($uid, $code) {
139 $r = $this->dataModel("USERLIST")->find(array("uid" => $uid));
140 if ($r->activationcode
== $code) {
141 $r->activationcode
= 0;
148 public function login($row) {
149 $dm = $this->dataModel("USER");
150 $w = $row->toArray();
153 if ($dm->rows() == 0) {
154 WidgetInterface
::addError("login_failed");
157 $dm->getRow(0)->status
= 1;//$this->core()->getSetting("online", __class__);
161 /* ---- Convinience functions ---- */
162 public function status() {
163 if ($this->dataModel("USER")->rows() > 0) {
164 return $this->dataModel("USER")->getRow(0)->status
;
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()) {
181 return $this->dataModel("USER")->getRow(0)->level
;
184 public function groups() {
185 if (!$this->online()) {
188 return $this->dataModel("USER")->getRow(0)->groups
;
191 public function inGroup($group) {
192 if (!$this->online()) {
195 return in_array($group, $this->groups());
198 public function userInfo() {
199 return $this->dataModel("USER")->getRow(0);