]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/BaculumUsersManager.php
baculum: Prevent opening new sessions for each request
[bacula/bacula] / gui / baculum / protected / Class / BaculumUsersManager.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2015 Marcin Haba
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 Prado::using('System.Security.IUserManager');
24 Prado::using('Application.Class.BaculumUser');
25
26 class BaculumUsersManager extends TModule implements IUserManager {
27
28         private $config;
29         private $configMod;
30         private $users;
31
32         public function init($config) {
33                 $this->configMod = $this->Application->getModule('configuration');
34                 $this->config = $this->configMod->isApplicationConfig() ? $this->configMod->getApplicationConfig() : null;
35                 $this->users = $this->configMod->getAllUsers();
36         }
37
38         public function getGuestName() {
39                 return 'guest';
40         }
41
42         public function validateUser($username, $password) {
43                 $valid = false;
44                 if(!empty($username) && !empty($password)) {
45                         $users = $this->configMod->getAllUsers();
46                         $valid = (array_key_exists($username, $users) && $password === $users[$username]);
47                 }
48                 return $valid;
49         }
50
51         public function getUser($username = null) {
52                 $user = new BaculumUser($this);
53                 $user->setIsGuest(false);
54                 $id = sha1(time());
55                 $user->setID($id);
56                 $user->setName($username);
57                 if (!is_null($username)) {
58                         $user->setPwd($this->users[$username]);
59                 }
60                 if(is_null($this->config) || $this->config['baculum']['login'] === $username) {
61                         $user->setRoles('admin');
62                 } else {
63                         $user->setRoles('user');
64                 }
65                 return $user;
66         }
67
68         public function getUserFromCookie($cookie) {
69                 $data = $cookie->Value;
70                 if (!empty($data)) {
71                         $data = $this->Application->SecurityManager->validateData($data);
72                         if ($data != false) {
73                                 $data = unserialize($data);
74                                 if (is_array($data) && count($data) === 3) {
75                                         list($username, $address, $token) = $data;
76                                         return $this->getUser($username);
77                                 }
78                         }
79                 }
80         }
81
82         public function saveUserToCookie($cookie) {
83                 $address = $this->Application->Request->UserHostAddress;
84                 $username = $this->User->getName();
85                 $token = $this->User->getID();
86                 $data = array($username, $address, $token);
87                 $data = serialize($data);
88                 $data = $this->Application->SecurityManager->hashData($data);
89                 $cookie->setValue($data);
90         }
91
92         public function loginUser($user = null, $pwd = null) {
93                 if (is_null($user) && is_null($pwd)) {
94                         $user = $_SERVER['PHP_AUTH_USER'];
95                         $pwd = $this->Application->getModule('configuration')->getCryptedPassword($_SERVER['PHP_AUTH_PW']);
96                 }
97                 $logged = $this->Application->getModule('auth')->login($user, $pwd, 86400);
98                 return $logged;
99         }
100 }
101 ?>