Paginator: Various fixes
[AOOS.git] / modules / User / User.php
blob0159be9179fd2e107bcdc4faf6c458825962e587
1 <?php
3 /**
4 * User-module
5 * This is one of two parts of the User-module.
6 * The other part is the UserHandler-class defined in modules/User/UserHandler.php
7 * The reason for this structure is that it eases the handling of User-data.
8 * Other modules can copy this structure if it is useful in the particular module.
9 * This would mostly be modules that contains the same data for a long time.
11 * @TODO Should UserHandler be a module for it self?
14 class User extends AOOSModule
16 private $_accesLevel = null;
17 private $_levels = array(
18 "guest" => 1,
19 "user" => 2,
20 "moderator" => 4,
21 "admin" => 8
23 private $_groups = array();
25 private $_username = "Guest";
26 private $_password = null;
27 private $_email = null;
29 private $_loggedIn = false;
31 public function show() {
32 $r = $this->core()->getModule("Reciever");
33 $p = $this->core()->getModule("Paginator");
34 require_once("UserHandler.php");
35 $handler = new UserHandler($this->core());
37 if ($p->numOptions() > 0) {
38 switch (array_shift($p->options())) {
39 case("create"):
40 if (!$r->getModel("POST")) {
41 return '<form method="POST">
42 Brugernavn: <input type="text" name="USERNAME" /><br />
43 Kodeord: <input type="text" name="PASSWORD" /><br />
44 E-mail: <input type="text" name="EMAIL" /><br />
45 <input type="submit" value="Opret" />
46 </form>';
48 else {
49 $m = $r->getModel("POST");
50 if (!$handler->createUser($m)) {
51 return 'Creation failed!';
53 return 'User created';
55 break;
56 case("activate"):
57 if (!$r->getModel("POST")) {
58 return '<form method="POST">
59 Brugernavn: <input type="text" name="USERNAME" /><br />
60 Kodeord: <input type="text" name="PASSWORD" /><br />
61 <input type="submit" value="Aktiver" />
62 </form>';
64 else {
65 $m = $r->getModel("POST");
66 if (!$handler->activateUser($m)) {
67 return 'Activation failed!';
69 return 'User activated';
71 break;
72 default:
73 break;
77 if (!$this->loggedIn()) {
78 if (!$r->getModel("POST")) {
79 return '
80 <form method="POST">
81 Brugernavn: <input type="text" name="USERNAME" /><br />
82 Kodeord: <input type="text" name="PASSWORD" /><br />
83 <input type="submit" value="Login" /><a href="'.$p->createURL("User", array("create")).'">Opret</a>
84 </form>';
86 else {
87 $m = $r->getModel("POST");
88 if (!$handler->login($m)) {
89 return "Login failed!";
93 else {
94 return "Logged in as ".$this->username();
98 public function setUsername($username)
100 $this->_username = $username;
101 return true;
104 public function setPassword($password)
106 $this->_password = $password;
107 return true;
110 public function setEmail($email)
112 $this->_email = $email;
113 return true;
116 public function username() { return $this->_username; }
117 public function password() { return $this->_password; }
118 public function email() { return $this->_email; }
120 public function addToGroup($group) {
121 $this->_groups[] = $group;
122 return true;
125 public function setGroups($groups) {
126 $this->_groups = $groups;
127 return true;
130 public function checkGroup($group) {
131 if (!in_array($group, $this->_groups)) {
132 return false;
134 return true;
137 public function setLevel($level) {
138 $this->_accessLevel = $level;
139 return true;
142 public function checkLevel($level) {
143 if ($this->_levels[$level] > $this->_accessLevel) {
144 return false;
146 return true;
149 public function setLoggedIn($state) {
150 if (!is_bool($state)) {
151 throw new AOOSException($this->core(), $this->tr("not_bool"), "", true, 1);
152 return false;
154 $this->_loggedIn = $state;
157 public function loggedIn() {
158 return $this->_loggedIn;