summaryrefslogtreecommitdiff
path: root/utils/modules/users.php
blob: 7e6fcd92a28377247a7fda1a67c7f81d6ddc9f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR;

include_once($UTIL_DIR . "/user.php");

class UsersAdm {
	public $users;

  // Admin config
  public $admin_title = "Users";
  public $admin_submodules = array("Add user" => "add",
                                   "Edit user" => "edit",
                                   "Delete user" => "delete");

  public function admin_add($action, $vars)
  {
    global $UID, $GLOBALS;

    switch($action) {
		case "add":
      $modulesel = $GLOBALS["modulesel"];

      $modstr = "";
      foreach($modulesel as $m) {
        if($modstr != "") $modstr .= " ";
        $modstr .= $m;
      }

      $all = true;
      foreach(getModuleList() as $m) {
        if(in_array($m, $modulesel) == false) $all = false;
      }
      
      if($all == true) $modstr = "*";

      $pwd = sha1(md5($vars["password"]));
      $user = new User($vars["username"], $pwd, $modstr);
      $this->users->add($user);
      $this->users->write();
      break;

    default:
      $modules = array();
      foreach(getModuleList() as $m) {
        $modules[$m] = false;
      }
			$form = new Form("add");
			$form->addWidget(new LineEdit("Username:", "username", ""));
			$form->addWidget(new LineEditPwd("Password:", "password", ""));
      $form->addWidget(new MultiList("Modules:", "modulesel",
                                     $modules));
      $form->addWidget(new Button("Add"));
			$form->render();
      break;
    }
  }

  public function admin_edit($action, $vars)
  {
    global $UID, $GLOBALS;

    switch($action) {
		case "write":
      $modulesel = $GLOBALS["modulesel"];

      $modstr = "";
      foreach($modulesel as $m) {
        if($modstr != "") $modstr .= " ";
        $modstr .= $m;
      }

      $all = true;
      foreach(getModuleList() as $m) {
        if(in_array($m, $modulesel) == false) $all = false;
      }
      
      if($all == true) $modstr = "*";

      $user = $this->users->findUser($vars["edtuser"]);

      if($vars["password"] != "") {
        $pwd = sha1(md5($vars["password"]));
        $user->password = $pwd;
      }
      $user->modules = $modstr;
      $this->users->write();
      break;

    case "edit":

      $user = $this->users->findUser($vars["edtuser"]);

      $modules = array();
      foreach(getModuleList() as $m) {
        $modules[$m] = $user->checkModule($m);
      }
			$form = new Form("write");
			$form->addWidget(new Hidden(array("edtuser" => $user->userid)));
			$form->addWidget(new LineEditPwd("Password:", "password", ""));
      $form->addWidget(new MultiList("Modules:", "modulesel",
                                     $modules));
      $form->addWidget(new Button("Edit"));
			$form->render();
      break;

    default:
      $userlist = array();
      
      foreach($this->users->useridList() as $u) {
        $userlist[$u] = $u;
      }

			$form = new Form("edit");
      $form->addWidget(new ComboBox("Select user to edit:", "edtuser", "",
                                    $userlist));
      $form->addWidget(new Button("Edit..."));
			$form->render();
      break;
    }
  }

  public function admin_delete($action, $vars)
  {
    global $UID, $GLOBALS;

    switch($action) {
		case "delete":
      if($vars["deluser"] == "admin") {
        echo "<p>ERROR: You cannot delete the 'admin' user.</p>";
        return;
      } 
      $this->users->delete($vars["deluser"]);
      $this->users->write();
      break;

    default:
      $userlist = array();
      
      foreach($this->users->useridList() as $u) {
        $userlist[$u] = $u;
      }

			$form = new Form("delete");
      $form->addWidget(new ComboBox("Select user to delete:", "deluser", "",
                                    $userlist));
      $form->addWidget(new Button("Delete"));
			$form->render();
      break;
    }
  }

  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "add":
      $this->admin_add($action, $vars);
      break;
    case "edit":
      $this->admin_edit($action, $vars);
      break;
    case "delete":
      $this->admin_delete($action, $vars);
      break;
    }
  }


	public function UsersAdm($file)
	{
    $this->users = new Users($file);
	}
}

function users_init()
{
  global $DATA_DIR;
  return new UserSAdm($DATA_DIR . "/users.xml");
}

?>