-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManager.cpp
More file actions
434 lines (356 loc) · 9.31 KB
/
Copy pathUserManager.cpp
File metadata and controls
434 lines (356 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "UserManager.h"
#include "MailManager.h"
#include "IdentityManager.h"
#include <algorithm>
#include <memory>
#include <utility>
#include <libutils/FileUtils.h>
#include <libopi/JsonHelper.h>
#include <libutils/Logger.h>
#include <libutils/Process.h>
#include <libopi/SysConfig.h>
#include <libopi/Secop.h>
#define SCFG (OPI::SysConfig())
using namespace OPI;
using namespace Utils;
using namespace std;
namespace KGP {
UserManager::UserManager(SecopPtr authdb):authdb(std::move(authdb))
{
if( ! this->authdb )
{
this->authdb = std::make_shared<Secop>();
this->authdb->SockAuth();
}
}
UserManagerPtr KGP::UserManager::Instance(SecopPtr authdb)
{
return UserManagerPtr(new UserManager(std::move(authdb)) );
}
bool UserManager::UserExists(const string &username)
{
vector<string> users = this->authdb->GetUsers();
return std::find(users.begin(), users.end(), username) != users.end();
}
bool UserManager::AddUser(const string &username, const string &password, const string &displayname, bool isAdmin, const map<string, string> &attributes)
{
if( ! this->authdb->CreateUser(username, password, displayname) )
{
this->global_error = "Failed to create user (User exists?)";
return false;
}
for( const pair<const string, string>& attr: attributes )
{
this->authdb->AddAttribute( username, attr.first, attr.second );
}
MailManager &mmgr = MailManager::Instance();
// Set local mail address
if( ! mmgr.SetLocalAddress( username ) )
{
this->global_error = mmgr.StrError();
return false;
}
if( isAdmin )
{
if( ! this->authdb->AddGroupMember("admin", username) )
{
this->global_error = "Failed to make user admin";
return false;
}
// Add this user as receiver of administrative mail
if( !mmgr.AddToAdmin(username))
{
this->global_error = mmgr.StrError();
return false;
}
}
// Add default email
IdentityManager &idmgr = IdentityManager::Instance();
string host, domain;
if( idmgr.HasDnsProvider() )
{
tie(host, domain) = idmgr.GetCurrentDnsName();
}
else
{
logg << Logger::Notice << "No dns-provider available, assigning local mail address" << lend;
tie(host,domain) = idmgr.GetFqdn();
}
if( host!="" && domain != "" )
{
logg << Logger::Debug << "Adding address " << username << "@"<< host << "."<<domain << " to " << username << lend;
mmgr.SetAddress(host+"."+domain, username, username);
}
else
{
logg << Logger::Notice << "No valid hostname, not adding email address to user " << username << lend;
}
if( !mmgr.Synchronize() )
{
this->global_error = mmgr.StrError();
return false;
}
return true;
}
bool UserManager::AddUser(const UserPtr& user, const string &password, bool isAdmin)
{
if( user == nullptr )
{
return false;
}
return this->AddUser( user->GetUsername(), password, user->GetDisplayname(), isAdmin, user->GetAttributes());
}
UserPtr UserManager::GetUser(const string &username)
{
if( ! this->UserExists( username ) )
{
this->global_error = "User not found";
return nullptr;
}
string displayname;
try {
displayname = this->authdb->GetAttribute(username, "displayname");
} catch (std::runtime_error& err) {
logg << Logger::Info << "Missing displayname for "<< username << " ("<< err.what()<<")"<<lend;
}
UserPtr user = std::make_shared<User>(username, displayname );
vector<string> attrs = this->authdb->GetAttributes( username );
for(const string& attr: attrs)
{
if( attr != "displayname" )
{
user->AddAttribute(attr, this->authdb->GetAttribute(username, attr) );
}
}
return user;
}
bool UserManager::UpdateUser(const UserPtr& user)
{
if( user == nullptr || user->GetUsername() == "" )
{
this->global_error = "Missing user object or username";
return false;
}
if( ! this->authdb->AddAttribute( user->GetUsername(), "displayname", user->GetDisplayname() ) )
{
this->global_error = "Failed to update user";
return false;
}
for( const pair<const string,string>& attr: user->GetAttributes() )
{
this->authdb->AddAttribute( user->GetUsername(), attr.first, attr.second);
}
return true;
}
bool UserManager::DeleteUser(const string &user)
{
if( ! this->authdb->RemoveUser( user ) )
{
this->global_error = "Failed to remove user: " + user;
logg << Logger::Error << this->global_error << lend;
return false;
}
// Remove all mail
MailManager& mmgr = MailManager::Instance();
mmgr.DeleteUser( user );
mmgr.Synchronize();
// delete the users files and mail
logg << Logger::Debug << "Deleting files for user: " << user << lend;
try {
string storage = SCFG.GetKeyAsString("filesystem","storagemount");
string dir = storage + "/mail/data/" + user;
if( File::DirExists(dir.c_str()))
{
Process::Exec("rm -rf "+ dir);
}
dir = storage + "/nextcloud/data/" + user;
if( File::DirExists(dir.c_str()))
{
Process::Exec("rm -rf "+ dir);
}
}
catch (std::runtime_error& e)
{
logg << Logger::Error << "Failed to delete user files" << e.what() << lend;
return false;
}
return true;
}
bool UserManager::DeleteUser(const UserPtr& user)
{
if( user == nullptr || user->GetUsername() == "")
{
this->global_error = "No user object provided or empty user";
return false;
}
return this->DeleteUser( user->GetUsername() );
}
bool UserManager::UpdateUserPassword(const string &user, const string &new_pass, const string &old_pass)
{
list<map<string,string>> ids = this->authdb->GetIdentifiers( user, "opiuser");
if(ids.size() == 0 )
{
this->global_error = "Database error";
return false;
}
map<string,string> id = ids.front();
if( id.find("password") == id.end() )
{
this->global_error = "Database error";
return false;
}
if( old_pass != "" )
{
// We should check old password against db
if( old_pass != id["password"] )
{
this->global_error = "Bad request";
return false;
}
}
if( ! this->authdb->UpdateUserPassword( user, new_pass) )
{
this->global_error = "Operation failed";
return false;
}
return true;
}
list<UserPtr> UserManager::GetUsers()
{
list<UserPtr> users;
vector<string> usernames = this->authdb->GetUsers();
for(const auto& user: usernames)
{
users.push_back(this->GetUser( user ) );
}
return users;
}
list<string> UserManager::GetGroups()
{
vector<string> groups = this->authdb->GetGroups();
return list<string>(groups.begin(), groups.end());
}
list<string> UserManager::GetUserGroups(const string &user)
{
vector<string> groups = this->authdb->GetUserGroups( user );
return list<string>(groups.begin(), groups.end());
}
bool UserManager::AddGroup(const string &groupname)
{
if( ! this->authdb->AddGroup( groupname) )
{
this->global_error = "Failed to add new group";
return false;
}
return true;
}
bool UserManager::DeleteGroup(const string &groupname)
{
if( ! this->authdb->RemoveGroup( groupname) )
{
this->global_error = "Failed to delete group";
return false;
}
return true;
}
bool UserManager::AddGroupMember(const string &group, const string &member)
{
if( ! this->authdb->AddGroupMember( group, member) )
{
this->global_error = "Failed to add member to group";
return false;
}
return true;
}
bool UserManager::DeleteGroupMember(const string &group, const string &member)
{
if( ! this->authdb->RemoveGroupMember(group, member) )
{
this->global_error = "Failed to remove member from group";
return false;
}
if( group == "admin" )
{
// User left admin role, remove from admin mail as well
MailManager& mmgr = MailManager::Instance();
if( ! mmgr.RemoveFromAdmin( member ) )
{
this->global_error ="Failed to remove user from admin mail";
return false;
}
}
return true;
}
list<string> UserManager::GetGroupMembers(const string &group)
{
vector<string> members = this->authdb->GetGroupMembers(group);
return list<string>(members.begin(), members.end());
}
/*
*
* User implementation
*
*/
User::User(string username, string displayname, map<string, string> attrs): username(std::move(username)), displayname(std::move(displayname)),attributes(std::move(attrs))
{
}
User::User(const json &userdata)
{
JsonHelper::TypeChecker tc( {
{0x01, "username", JsonHelper::TypeChecker::STRING},
{0x02, "displayname", JsonHelper::TypeChecker::STRING}
} );
if( ! tc.Verify( 0x01|0x02, userdata ) )
{
throw std::runtime_error("Missing parameters in user constructor");
}
this->username = userdata["username"].get<string>();
this->displayname = userdata["displayname"].get<string>();
//vector<string> members = userdata.getMemberNames();
for( const auto& member: userdata.items())
{
if( member.key() != "username" && member.key() != "displayname" )
{
if( member.value().is_string() )
{
this->AddAttribute(member.key(), member.value().get<string>() );
}
else
{
logg << Logger::Debug << "Malformed attribute to create user " << member.key()<< lend;
}
}
}
}
string User::GetUsername()
{
return this->username;
}
string User::GetDisplayname()
{
return this->displayname;
}
void User::AddAttribute(const string &attr, const string& value)
{
this->attributes[attr] = value;
}
string User::GetAttribute(const string &attr)
{
if( this->attributes.find(attr) == this->attributes.end() )
{
throw std::runtime_error("Unable to find attribute");
}
return this->attributes[attr];
}
map<string, string> User::GetAttributes()
{
return this->attributes;
}
json User::ToJson()
{
json ret;
ret["username"] = this->username;
ret["displayname"] = this->displayname;
return ret;
}
} // Namespace KGP