]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/BaculumUsersManager.php
baculum: Assign Baculum copyright to Kern Sibbald
[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-2016 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 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
31         public function init($config) {
32                 $this->configMod = $this->Application->getModule('configuration');
33                 $this->config = $this->configMod->isApplicationConfig() ? $this->configMod->getApplicationConfig() : null;
34         }
35
36         public function getGuestName() {
37                 return 'guest';
38         }
39
40         public function validateUser($username, $password) {
41                 $valid = false;
42                 if(!empty($username) && !empty($password)) {
43                         $users = $this->configMod->getAllUsers();
44                         $valid = (array_key_exists($username, $users) && $password === $users[$username]);
45                 }
46                 return $valid;
47         }
48
49         public function getUser($username = null) {
50                 $user = new BaculumUser($this);
51                 $user->setIsGuest(false);
52                 $id = sha1(time());
53                 $user->setID($id);
54                 $user->setName($username);
55                 if (!is_null($username)) {
56                         $users = $this->configMod->getAllUsers();
57                         $user->setPwd($users[$username]);
58                 }
59                 if(is_null($this->config) || $this->config['baculum']['login'] === $username) {
60                         $user->setRoles('admin');
61                 } else {
62                         $user->setRoles('user');
63                 }
64                 return $user;
65         }
66
67         public function getUserFromCookie($cookie) {
68                 $data = $cookie->Value;
69                 if (!empty($data)) {
70                         $data = $this->Application->SecurityManager->validateData($data);
71                         if ($data != false) {
72                                 $data = unserialize($data);
73                                 if (is_array($data) && count($data) === 3) {
74                                         list($username, $address, $token) = $data;
75                                         return $this->getUser($username);
76                                 }
77                         }
78                 }
79         }
80
81         public function saveUserToCookie($cookie) {
82                 $address = $this->Application->Request->UserHostAddress;
83                 $username = $this->User->getName();
84                 $token = $this->User->getID();
85                 $data = array($username, $address, $token);
86                 $data = serialize($data);
87                 $data = $this->Application->SecurityManager->hashData($data);
88                 $cookie->setValue($data);
89         }
90
91         public function loginUser($user = null, $pwd = null) {
92                 if (is_null($user) && is_null($pwd) && isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
93                         $user = $_SERVER['PHP_AUTH_USER'];
94                         $pwd = $this->Application->getModule('configuration')->getCryptedPassword($_SERVER['PHP_AUTH_PW']);
95                 }
96                 $logged = $this->Application->getModule('auth')->login($user, $pwd, 86400);
97                 return $logged;
98         }
99 }
100 ?>