]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/Users.php
baculum: Fix redundant loading users portlet
[bacula/bacula] / gui / baculum / protected / Web / Portlets / Users.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 Kern Sibbald
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The original author of Bacula is Kern Sibbald, with contributions
10  * from many others, a complete list can be found in the file AUTHORS.
11  *
12  * You may use this file and others of this release according to the
13  * license defined in the LICENSE file, which includes the Affero General
14  * Public License, v3.0 ("AGPLv3") and some additional permissions and
15  * terms pursuant to its AGPLv3 Section 7.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23
24 Prado::using('System.Web.UI.ActiveControls.TActiveRepeater');
25 Prado::using('Application.Web.Portlets.Portlets');
26
27 class Users extends Portlets {
28
29         public $web_config;
30
31         public function __construct() {
32                 parent::__construct();
33                 $this->web_config = $this->getModule('web_config')->getConfig();
34         }
35
36         public function setUsers() {
37                 if(!$_SESSION['admin']) {
38                         return;
39                 }
40                 $all_users = $this->getModule('basic_webuser')->getAllUsers();
41                 $users = array_keys($all_users);
42                 sort($users);
43                 $users_list = array();
44                 $users_feature = (array_key_exists('users', $this->web_config) && is_array($this->web_config['users']));
45                 for ($i = 0; $i < count($users); $i++) {
46                         $host = null;
47                         if ($users_feature && array_key_exists($users[$i], $this->web_config['users'])) {
48                                 $host = $this->web_config['users'][$users[$i]];
49                         }
50                         $users_list[] = array(
51                                 'user' => $users[$i],
52                                 'host' => $host,
53                                 'admin' => ($users[$i] === $this->web_config['baculum']['login'])
54                         );
55                 }
56                 $this->UsersList->dataSource = $users_list;
57                 $this->UsersList->dataBind();
58         }
59
60         public function initHosts($sender, $param) {
61                 $api_hosts = array_keys($this->getModule('host_config')->getConfig());
62                 $sender->DataSource = array_combine($api_hosts, $api_hosts);
63                 $sender->dataBind();
64         }
65
66         public function userAction($sender, $param) {
67                 if(!$_SESSION['admin']) {
68                         return;
69                 }
70                 list($action, $user, $value) = explode(';', $param->CallbackParameter, 3);
71                 switch($action) {
72                         case 'newuser':
73                         case 'chpwd': {
74                                         $admin = false;
75                                         $valid = true;
76                                         if ($user === $this->web_config['baculum']['login']) {
77                                                 $this->web_config['baculum']['password'] = $value;
78                                                 $valid = $this->getModule('web_config')->setConfig($this->web_config);
79                                                 $admin = true;
80                                         }
81                                         if ($valid === true) {
82                                                 $this->getModule('basic_webuser')->setUsersConfig($user, $value);
83                                         }
84                                         if ($admin === true) {
85                                                 // if admin password changed then try to auto-login by async request
86                                                 $http_protocol = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
87                                                 $this->switchToUser($user, $value);
88                                                 exit();
89                                         } else {
90                                                 // if normal user's password changed then update users grid
91                                                 $this->setUsers();
92                                         }
93                                 }
94                                 break;
95                         case 'rmuser': {
96                                         if ($user != $_SERVER['PHP_AUTH_USER']) {
97                                                 $this->getModule('basic_webuser')->removeUser($user);
98                                                 if (array_key_exists('users', $this->web_config) && array_key_exists($user, $this->web_config['users'])) {
99                                                         unset($this->web_config['users'][$user]);
100                                                 }
101                                                 $this->getModule('web_config')->setConfig($this->web_config);
102                                                 $this->setUsers();
103                                         }
104                                 break;
105                                 }
106                         case 'set_host': {
107                                         if (empty($value) && array_key_exists($user, $this->web_config['users'])) {
108                                                 unset($this->web_config['users'][$user]);
109                                         } else {
110                                                 $this->web_config['users'][$user] = $value;
111                                         }
112                                         $this->getModule('web_config')->setConfig($this->web_config);
113                                 break;
114                                 }
115                 }
116         }
117 }