|
1
|
|
|
<?php |
|
2
|
|
|
namespace SOG\Dashboard; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use Silex\Application; |
|
6
|
|
|
use Silex\ControllerCollection; |
|
7
|
|
|
use Silex\ControllerProviderInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Zend\Ldap\Attribute; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Simple extension to the @see \RedirectResponse class to easily redirect to the request referer. |
|
14
|
|
|
* |
|
15
|
|
|
* Class RefererRedirectResponse |
|
16
|
|
|
* @package SOG\Dashboard |
|
17
|
|
|
*/ |
|
18
|
|
|
class RefererRedirectResponse extends RedirectResponse |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string The default redirect route if the referer is empty |
|
22
|
|
|
*/ |
|
23
|
|
|
private $default_route = '/members/Mitglieder-verwalten'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Call the parent constructor to redirect appropriately |
|
27
|
|
|
* |
|
28
|
|
|
* @param Request $request |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Request $request) |
|
31
|
|
|
{ |
|
32
|
|
|
// referer might be empty, let's be certain and provide a default |
|
33
|
|
|
parent::__construct($request->headers->get('referer', $this->default_route)); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* This controller provides two endpoints for subscribing and unsubscribing guests to a given group. |
|
39
|
|
|
* The given attributes for a guest are either used to retrieve him/her from the LDAP tree or create a new guest. |
|
40
|
|
|
* This happens transparently and isn't the concern of the application itself. |
|
41
|
|
|
* |
|
42
|
|
|
* Class GuestControllerProvider |
|
43
|
|
|
* @package SOG\Dashboard |
|
44
|
|
|
*/ |
|
45
|
|
|
class GuestControllerProvider implements ControllerProviderInterface |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @var Application Reference to the Silex app for easy access to the services etc. |
|
49
|
|
|
*/ |
|
50
|
|
|
private $app; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function connect(Application $app) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->app = $app; |
|
58
|
|
|
|
|
59
|
|
|
/** @var ControllerCollection $controllers */ |
|
60
|
|
|
$controllers = $app['controllers_factory']; |
|
61
|
|
|
|
|
62
|
|
|
// used to subscribe a guest to a group mailing list |
|
63
|
|
|
$controllers->post('/subscribe', [$this, 'subscribe'])->before([$this, 'isAllowed']); |
|
64
|
|
|
|
|
65
|
|
|
return $controllers; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Checks if the value for `ou` is indeed one of the owned groups by the logged in user. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Request $request |
|
72
|
|
|
* @param Application $app |
|
73
|
|
|
* @return null|RefererRedirectResponse Redirects to the previous page if the user doesn't have permission. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function isAllowed(Request $request, Application $app) |
|
76
|
|
|
{ |
|
77
|
|
|
// checks ahead of executing the route callback if the given request is valid as in the user is an owner |
|
78
|
|
|
/** @var \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token */ |
|
79
|
|
|
$token = $this->app['security.token_storage']->getToken(); |
|
80
|
|
|
|
|
81
|
|
|
$ownerPermission = false; |
|
82
|
|
|
|
|
83
|
|
|
if (null !== $token) { |
|
84
|
|
|
$user = $token->getUser(); |
|
85
|
|
|
$ownedGroups = $app['ldap']->getOwnedGroups($user->getAttributes()['dn'])->toArray(); |
|
86
|
|
|
|
|
87
|
|
|
$selGroup = $request->request->get('ou'); |
|
88
|
|
|
if (!isset($selGroup)) $selGroup = $ownedGroups[0]['ou'][0]; |
|
89
|
|
|
$selGroupDN = sprintf('ou=%s,ou=groups,o=sog-de,dc=sog', $selGroup); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($ownedGroups as $og) { |
|
92
|
|
|
if ($og['dn'] == $selGroupDN) { |
|
93
|
|
|
$ownerPermission = true; |
|
94
|
|
|
break; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// fail, no permission granted! |
|
100
|
|
|
if ($ownerPermission === false) { |
|
101
|
|
|
return new RefererRedirectResponse($request); |
|
102
|
|
|
} |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Tries to subscribe the specified guest to the mailing list. |
|
108
|
|
|
* |
|
109
|
|
|
* @param Application $app |
|
110
|
|
|
* @param Request $request |
|
111
|
|
|
* @return RefererRedirectResponse Redirects to the previously visited page. |
|
112
|
|
|
*/ |
|
113
|
|
|
public function subscribe(Application $app, Request $request) |
|
114
|
|
|
{ |
|
115
|
|
|
$name = $request->request->get('name'); |
|
116
|
|
|
$mail = $request->request->get('mail'); |
|
117
|
|
|
$group = $request->request->get('ou'); |
|
118
|
|
|
|
|
119
|
|
|
if (is_null($name) || is_null($mail) || is_null($group)) { |
|
120
|
|
|
$app['session']->getFlashBag()->add('error', 'Der Gast konnte nicht gefunden und daher nicht der Liste hinzugefügt werden.'); |
|
121
|
|
|
return new RefererRedirectResponse($request); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (strpos($mail, '@studieren-ohne-grenzen.org') !== false) { |
|
125
|
|
|
$app['session']->getFlashBag()->add('error', 'Bitte füge SOG-Mitglieder nicht als Gäste hinzu.'); |
|
126
|
|
|
return new RefererRedirectResponse($request); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$user_dn = $this->retrieveGuestByMail($mail); |
|
130
|
|
|
if ($user_dn === false) { |
|
131
|
|
|
$info = $app['ldap']->createGuest($name, $mail); |
|
132
|
|
|
$user_dn = Attribute::getAttribute($info, 'dn', 0); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$group_dn = sprintf('ou=%s,ou=groups,o=sog-de,dc=sog', $group); |
|
136
|
|
|
if ($app['ldap']->isMemberOfGroup($user_dn, $group_dn)) { |
|
137
|
|
|
$app['session']->getFlashBag()->add('info', 'Der Gast ist bereits auf der Liste eingetragen.'); |
|
138
|
|
|
} else { |
|
139
|
|
|
$app['ldap']->addToGroup($user_dn, $group_dn); |
|
140
|
|
|
$app['session']->getFlashBag()->add('success', 'Der Gast wurde der Liste hinzugefügt.'); |
|
141
|
|
|
} |
|
142
|
|
|
return new RefererRedirectResponse($request); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Retrieves a guest by the given mail address. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $mail |
|
149
|
|
|
* @return false|string The retrieved DN of the guest or false if not found. |
|
150
|
|
|
*/ |
|
151
|
|
|
private function retrieveGuestByMail($mail) |
|
152
|
|
|
{ |
|
153
|
|
|
$info = $this->app['ldap']->getMemberByMail($mail, 'mail'); |
|
154
|
|
|
if (is_array($info)) |
|
155
|
|
|
return $info['dn']; |
|
156
|
|
|
else |
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
} |