News: minor fixes.
[AOOS.git] / modules / User / User.php
blob491bd704f5d8447b3949d13d128d1a5a1babbfb5
1 <?php
3 /**
4 * User-module
5 */
7 class User extends AOOSModule {
8 static public function dependencies() {
9 return array("Paginator", "Form", "Reciever");
12 /**
13 * Model m1 contains information on the current user. If the user isn't logged in, the model is empty(maybe some
14 * default values later on). When the user logs in, data is fetched from the database and the model contains a
15 * single row with all of the user's information.
16 * Model m1 is also used when a new user is created. When that happens, data is inserted into a new row in the model
17 * and save is called on the model.
18 * Model m2 contains the uid and the username of all users in the database. This ensures that usernames can be
19 * pulled quickly when you have the uid of the user.
21 public function dataModelDefinition() {
22 // Current user model
23 $m1 = new AOOSModel($this->core());
24 $m1->setSource("mysql");
25 $m1->setTable("User");
26 $m1->setColumnIndex(array("uid", "username", "password", "email", "signature", /*"avatar", */"active", "activationcode", "joined", "status", "groups", "level"));
28 $m1->setProperties("uid", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_PRIMARY_KEY|AOOSMODEL_FLAG_GUI_PRIVATE);
30 $m1->setProperties("username", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_ESCAPE|AOOSMODEL_PROP_NOHTML|AOOSMODEL_PROP_STRIP);
31 $m1->addConstraint("username", "Match", "/[a-zA-Z0-9-_]+/");
32 $m1->addConstraint("username", "Length", 50);
34 $m1->setProperties("password", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_HASH|AOOSMODEL_PROP_STRIP);
36 $m1->setProperties("email", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_ESCAPE|AOOSMODEL_PROP_NOHTML|AOOSMODEL_PROP_STRIP);
37 $m1->addConstraint("email", "Match", "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/"); // Match email
39 $m1->setProperties("signature", AOOSMODEL_TYPE_TEXT, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_ESCAPE|AOOSMODEL_PROP_NOHTML);
40 $m1->addConstraint("signature", "Length", 256);
42 // XXX $m1->setProperties("avatar", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE, AOOSMODEL_PROP_ESCAPE);
44 $m1->setProperties("active", AOOSMODEL_TYPE_BOOLEAN, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
45 $m1->setDefaultValue("active", false);
47 $m1->setProperties("activationcode", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
49 $m1->setProperties("joined", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE, AOOSMODEL_PROP_TIME);
51 $m1->setProperties("status", AOOSMODEL_TYPE_INTEGER,AOOSMODEL_FLAG_GUI_PRIVATE);
52 $m1->setDefaultValue("status",0);// $this->getStatusValue("offline"));
54 $m1->setProperties("groups", AOOSMODEL_TYPE_TEXT, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
56 $m1->setProperties("level", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
58 $m1->appendRow();
59 $m1->getRow(0)->setFlag(0);
61 // All users. Only the most important fields are fetched here
62 $m2 = new AOOSModel($this->core());
63 $m2->setSource("mysql");
64 $m2->setTable("User");
65 $m2->setColumnIndex(array("uid", "username", "status", "active", "activationcode"));
66 $m2->setProperties("uid", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_PRIMARY_KEY);
67 $m2->setProperties("username", AOOSMODEL_TYPE_STRING, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_UNIQUE, AOOSMODEL_PROP_ESCAPE|AOOSMODEL_PROP_NOHTML|AOOSMODEL_PROP_STRIP);
68 $m2->setProperties("active", AOOSMODEL_TYPE_BOOLEAN, AOOSMODEL_FLAG_FROM_DATABASE|AOOSMODEL_FLAG_GUI_PRIVATE);
69 $m2->setProperties("activationcode", AOOSMODEL_TYPE_INTEGER, AOOSMODEL_FLAG_FROM_DATABASE);
71 $a = array("USER" => $m1, "USERLIST" => $m2);
72 return $a;
75 public function show() {
76 $this->dataModel("USERLIST")->populate();
77 require_once("UserView.php");
78 $this->_view = new UserView($this);
79 $p = $this->core()->getModule("Paginator");
80 switch($p->getOption(0)) {
81 case("edit"):
82 return $this->_view->edit();
83 break;
84 case("add"):
85 return $this->_view->add();
86 break;
87 default:
88 return $this->_view->show();
89 break;
93 public function createUser($info) {
94 try {
95 $this->dataModel("USER")->appendRow($info->toArray());
96 } catch (AOOSException $e) {
97 throw $e;
99 // Appending was succesful
100 $this->dataModel("USER")->save();
101 return true;
104 public function updateUser($row) {
105 if (!$this->online()) {
106 return false;
108 foreach ($row->toArray() as $key => $val) {
109 $this->dataModel("USER")->getRow(0)->$key = $val;
111 $this->dataModel("USER")->save();
112 return true;
115 public function activateUser($row) {
116 return null;
119 public function login($row) {
120 $dm = $this->dataModel("USER");
121 $w = $row->toArray();
122 $w["active"] = true;
123 $dm->populate($w);
124 if ($dm->rows() == 0) {
125 return false;
127 $dm->getRow(0)->status = 1;//$this->core()->getSetting("online", __class__);
128 return true;
131 /* ---- Convinience functions ---- */
132 public function status() {
133 return $this->dataModel("USER")->getRow(0)->status;
136 public function getStatusValue($string) {
137 return $this->core()->getSetting("status_".$string, __class__);
140 public function online() {
141 return $this->status() == 1;//$this->getStatusValue("online");
144 public function level() {
145 if (!$this->online()) {
146 return false;
148 return $this->dataModel("USER")->getRow(0)->level;
151 public function groups() {
152 if (!$this->online()) {
153 return false;
155 return $this->dataModel("USER")->getRow(0)->groups;
158 public function inGroup($group) {
159 if (!$this->online()) {
160 return false;
162 return in_array($group, $this->groups());
165 public function userInfo() {
166 return $this->dataModel("USER")->getRow(0);