1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Steven Bühner |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
namespace HtpasswdManager\Service; |
8
|
|
|
|
9
|
|
|
use Zend\Http\Request; |
10
|
|
|
use Zend\EventManager\EventManagerAwareInterface; |
11
|
|
|
use Zend\EventManager\EventManagerInterface; |
12
|
|
|
|
13
|
|
|
class UserService implements EventManagerAwareInterface { |
14
|
|
|
protected $request; |
15
|
|
|
protected $not_deletable_users; |
16
|
|
|
protected $user_with_management_permission; |
17
|
|
|
protected $eventManager; |
18
|
|
|
|
19
|
|
|
public function __construct(Request $request, $not_deletable_users, $user_with_management_permission) { |
|
|
|
|
20
|
|
|
$this->request = $request; |
21
|
|
|
$this->not_deletable_users = $not_deletable_users; |
22
|
|
|
$this->user_with_management_permission = $user_with_management_permission; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Return the current used user |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function getCurrentUser() { |
31
|
|
|
return $this->request->getServer ()->get ( 'REMOTE_USER', null ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function isUserDeleteable($username) { |
|
|
|
|
35
|
|
|
$eResult = $this->getEventManager ()->trigger ( 'pre_' . __FUNCTION__, $this, array( |
36
|
|
|
$username |
37
|
|
|
) ); |
38
|
|
|
|
39
|
|
|
if ($eResult->stopped ()) |
40
|
|
|
return $eResult->last (); |
41
|
|
|
|
42
|
|
|
return ! in_array ( $username, $this->not_deletable_users ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function isUserAllowedToManageUsers($username) { |
|
|
|
|
46
|
|
|
$eResult = $this->getEventManager ()->trigger ( 'pre_' . __FUNCTION__, $this, array( |
47
|
|
|
'user' => $username |
48
|
|
|
) ); |
49
|
|
|
|
50
|
|
|
if ($eResult->stopped ()) |
51
|
|
|
return $eResult->last (); |
52
|
|
|
|
53
|
|
|
/* If parameter is an array => allow only users in list. IF parameter is boolean, return boolean value */ |
54
|
|
|
if (is_array ( $this->user_with_management_permission )) { |
55
|
|
|
$accessAllowed = in_array ( $username, $this->user_with_management_permission ); |
56
|
|
|
} else { |
57
|
|
|
$accessAllowed = ($this->user_with_management_permission === true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $accessAllowed; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setEventManager(EventManagerInterface $eventManager) { |
64
|
|
|
$eventManager->setIdentifiers ( array( |
65
|
|
|
__CLASS__, |
66
|
|
|
get_class ( $this ) |
67
|
|
|
) ); |
68
|
|
|
$this->eventManager = $eventManager; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getEventManager() { |
72
|
|
|
return $this->eventManager; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.