1
|
|
|
<?php |
2
|
|
|
namespace SOG\Api; |
3
|
|
|
|
4
|
|
|
use Silex\Application; |
5
|
|
|
use Silex\Provider\SwiftmailerServiceProvider; |
6
|
|
|
use SOG\Dashboard\DataUtilityServiceProvider; |
7
|
|
|
use SOG\Dashboard\GroupControllerProvider; |
8
|
|
|
use SOG\Dashboard\RandomStringServiceProvider; |
9
|
|
|
use SOG\Dashboard\ZendLdapServiceProvider; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class SogDashboardApi |
13
|
|
|
* |
14
|
|
|
* The external Dashboard API |
15
|
|
|
* |
16
|
|
|
* example usage: |
17
|
|
|
* |
18
|
|
|
* ```php |
19
|
|
|
* include $pathToConfigFile; // this will make the variable $config available |
20
|
|
|
* $api = new SogDashboardApi($config); |
21
|
|
|
* $username = $api->createUser($firstName, $lastName, $email, $group); |
22
|
|
|
* ``` |
23
|
|
|
* |
24
|
|
|
* @package SOG\Api |
25
|
|
|
*/ |
26
|
|
|
class SogDashboardApi |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Application The Silex application for the API |
30
|
|
|
*/ |
31
|
|
|
private $app; |
32
|
|
|
/** |
33
|
|
|
* @var string Full URL to the Dashboard application |
34
|
|
|
*/ |
35
|
|
|
private $dashboard_url = 'https://dashboard.studieren-ohne-grenzen.org'; |
36
|
|
|
/** |
37
|
|
|
* @var int The default length for a random user password |
38
|
|
|
*/ |
39
|
|
|
private $password_length = 8; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Instantiates a new LdapAdapter for creating and updating relevant entities. |
43
|
|
|
* |
44
|
|
|
* @param array $config The configuration as e.g. stored in the config.php file |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $config) |
47
|
|
|
{ |
48
|
|
|
$this->app = new Application(); |
49
|
|
|
|
50
|
|
|
// LdapAdapter is now available as $this->app['ldap'] |
51
|
|
|
$this->app->register(new ZendLdapServiceProvider(), [ |
52
|
|
|
'ldap.options' => $config['ldap.options'] |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
// SwiftMailer is now available as $this->app['mailer'] |
56
|
|
|
$this->app->register(new SwiftmailerServiceProvider()); |
57
|
|
|
$this->app['mailer.from'] = $config['mailer.from']; |
58
|
|
|
$this->app['swiftmailer.options'] = $config['swiftmailer.options']; |
59
|
|
|
$this->app['swiftmailer.use_spool'] = false; |
60
|
|
|
|
61
|
|
|
// can be used for passwords etc, by calling $this->app['random']($length = 8) |
62
|
|
|
$this->app->register(new RandomStringServiceProvider()); |
63
|
|
|
|
64
|
|
|
$this->app->register(new DataUtilityServiceProvider()); |
65
|
|
|
|
66
|
|
|
// used to notify group owners |
67
|
|
|
$this->app->mount('/groups', new GroupControllerProvider()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create a new user in the LDAP tree. Send notifications to the user and its group admin. |
73
|
|
|
* The account will be inactive by default. Memberships for the general group and the given $group |
74
|
|
|
* are also requested. |
75
|
|
|
* |
76
|
|
|
* @param string $firstName |
77
|
|
|
* @param string $lastName |
78
|
|
|
* @param string $email |
79
|
|
|
* @param string $group |
80
|
|
|
* @return string The new username. |
81
|
|
|
*/ |
82
|
|
|
public function createUser($firstName, $lastName, $email, $group) |
83
|
|
|
{ |
84
|
|
|
$username = $this->generateUsername($firstName, $lastName); |
85
|
|
|
$password = $this->app['random']($this->password_length); |
86
|
|
|
|
87
|
|
|
$data = $this->app['ldap']->createMember($username, $password, $firstName, $lastName, $email); |
88
|
|
|
|
89
|
|
|
$this->createSieveForwarding($data['mail'][0], $email); |
90
|
|
|
|
91
|
|
|
$this->requestGroupMembership($username, $group); |
92
|
|
|
$this->requestGroupMembership($username, 'allgemein'); |
93
|
|
|
|
94
|
|
|
$this->notifyNewUser($firstName, $username, $email, $password); |
95
|
|
|
$this->notifyNewUserAdmin($firstName, $lastName, $email, $group); |
96
|
|
|
|
97
|
|
|
return $username; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Generate a unique username by passing it to the LDAP adapter |
102
|
|
|
* which executes additional transformations and checks. |
103
|
|
|
* |
104
|
|
|
* @param string $firstName |
105
|
|
|
* @param string $lastName |
106
|
|
|
* @return string The unique username |
107
|
|
|
*/ |
108
|
|
|
private function generateUsername($firstName, $lastName) |
109
|
|
|
{ |
110
|
|
|
return $this->app['ldap']->generateUsername(trim($firstName) . " " . trim($lastName)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Shells out to a bash script to generate a sieve script for initial email forwarding. |
115
|
|
|
* This could be improved, for sure. Note that the apache user needs the NOPASSWD: tag in sudoers(5) |
116
|
|
|
* |
117
|
|
|
* @param string $from The mail address for the new member |
118
|
|
|
* @param string $to The personal mail address to forward messages to |
119
|
|
|
*/ |
120
|
|
|
private function createSieveForwarding($from, $to) |
121
|
|
|
{ |
122
|
|
|
$cmd_tpl = 'sudo %s/create_sieve_forwarding.sh %s %s'; |
123
|
|
|
$cmd = sprintf($cmd_tpl, __DIR__ . '/../..', escapeshellarg($from), escapeshellarg($to)); |
124
|
|
|
shell_exec($cmd); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Request membership in the given group for a user. |
129
|
|
|
* |
130
|
|
|
* @param string $uid The generated unique username for the member |
131
|
|
|
* @param string $group The CN of the group for which to request the membership |
132
|
|
|
* @return boolean True, if there isn't already an active request from the user for the group; false otherwise |
133
|
|
|
*/ |
134
|
|
|
public function requestGroupMembership($uid, $group) |
135
|
|
|
{ |
136
|
|
|
return $this->app['ldap']->requestGroupMembership($uid, $group); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Send a mail to the user. |
141
|
|
|
* This is send only for OpenAtrium account details, a welcome mail is send through CiviCRM! |
142
|
|
|
* |
143
|
|
|
* @param string $firstName |
144
|
|
|
* @param string $username |
145
|
|
|
* @param string $email |
146
|
|
|
* @param string $password |
147
|
|
|
*/ |
148
|
|
|
private function notifyNewUser($firstName, $username, $email, $password) |
149
|
|
|
{ |
150
|
|
|
$text = ' |
151
|
|
|
<html><head><title></title></head><body> |
152
|
|
|
Hallo ' . $firstName . ',<br /> |
153
|
|
|
Wir freuen uns sehr, dich als neues Mitglied bei Studieren Ohne Grenzen begrüßen zu dürfen.<br /> |
154
|
|
|
<br /> |
155
|
|
|
Damit du direkt einsteigen und mitarbeiten kannst, haben wir dir automatisch einen Zugang für unsere Online-Plattform erstellt. Über diese Plattform tauschen wir wichtige Nachrichten, Informationen und Dateien aus und diskutieren auch Lokalgruppen-übergreifend.<br /> |
156
|
|
|
<br /> |
157
|
|
|
Benutzername: ' . $username . '<br /> |
158
|
|
|
Passwort: ' . $password . '<br /> |
159
|
|
|
<br /> |
160
|
|
|
Dein Account wird freigeschaltet, sobald dein Lokalkoordinator bestätigt hat, dass du tatsächlich bei Studieren Ohne Grenzen aktiv bist.<br /> |
161
|
|
|
<br /> |
162
|
|
|
Mit diesen Zugangsdaten kannst du dich auf allen SOG-Systeme einloggen.<br /> |
163
|
|
|
<br /> |
164
|
|
|
Eine Übersicht deiner Daten und Gruppen gibt dir das Dashboard: https://dashboard.studieren-ohne-grenzen.org<br /> |
165
|
|
|
Viele Grüße,<br /> |
166
|
|
|
Das SOG-IT-Team |
167
|
|
|
</body> |
168
|
|
|
</html> |
169
|
|
|
'; |
170
|
|
|
$message = \Swift_Message::newInstance() |
171
|
|
|
->setSubject('[Studieren Ohne Grenzen] Zugangsdaten') |
172
|
|
|
->setFrom([$this->app['mailer.from']]) |
173
|
|
|
->setTo([$email => $firstName]) |
174
|
|
|
->setBody($text, 'text/html'); |
175
|
|
|
return $this->app['mailer']->send($message); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Send email to group administrator to inform about new member |
180
|
|
|
* |
181
|
|
|
* @param string $firstName |
182
|
|
|
* @param string $lastName |
183
|
|
|
* @param string $email |
184
|
|
|
* @param string $group |
185
|
|
|
* @return int Number of accepted recipients |
186
|
|
|
*/ |
187
|
|
|
private function notifyNewUserAdmin($firstName, $lastName, $email, $group) |
188
|
|
|
{ |
189
|
|
|
$text = "Soeben hat sich ein neues Mitglied fuer Deine Lokalgruppe angemeldet.<br> |
190
|
|
|
Das neue Mitglied ist schon auf dem Lokalgruppen-Verteiler eingetragen und hat einen Account für alle Services erhalten.<br><br> |
191
|
|
|
<b>Achtung:</b> Der SOG-Account des Mitglieds muss erst von dir aktiviert werden. Bitte bestätige am Besten jetzt <a href='" . $this->dashboard_url . "'>direkt im Dashboard</a>, dass " . $firstName . " " . $lastName . " tatsächlich in eurer LG aktiv ist! |
192
|
|
|
<br><br> |
193
|
|
|
Hier die Daten des neuen Mitglieds:<br>"; |
194
|
|
|
$text .= "Vorname: " . $firstName . "<br>"; |
195
|
|
|
$text .= "Nachname: " . $lastName . "<br>"; |
196
|
|
|
$text .= "Mail: " . $email . "<br>"; |
197
|
|
|
$text .= "Standort: " . $group . "<br>"; |
198
|
|
|
|
199
|
|
|
return $this->app['notify_owners']($group, '[Studieren Ohne Grenzen] Neuanmeldung in deiner Lokalgruppe', $text); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Update the mail-alternative field for the given UID |
204
|
|
|
* |
205
|
|
|
* @param string $uid The user id for which to update |
206
|
|
|
* @param string $email The new alternative mail address |
207
|
|
|
*/ |
208
|
|
|
public function updateAlternativeMail($uid, $email) |
209
|
|
|
{ |
210
|
|
|
$dn = $this->app['ldap']->findUserDN($uid); |
211
|
|
|
$this->app['ldap']->updateEmail($dn, $email); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|