|
|
@@ -0,0 +1,42 @@ |
|
|
|
module service.users; |
|
|
|
|
|
|
|
import config.global : GlobalConfig; |
|
|
|
import model.user : User; |
|
|
|
import jsonizer.tojson; |
|
|
|
|
|
|
|
import std.file : isDir, mkdir, write, FileException; |
|
|
|
import core.stdc.errno : EEXIST; |
|
|
|
|
|
|
|
import crypto.rsa; |
|
|
|
|
|
|
|
/** |
|
|
|
* Args: (string) username |
|
|
|
* Returns: (User) created user |
|
|
|
*/ |
|
|
|
string createUser(GlobalConfig conf, User caller, string username) { |
|
|
|
if (!caller.admin) |
|
|
|
throw new Exception("Only administrators can create a new user!"); |
|
|
|
|
|
|
|
User user; |
|
|
|
user.name = username; |
|
|
|
|
|
|
|
if (!isDir(user.getPath())) |
|
|
|
throw new Exception("The user directory doesn't exist! " ~ user.getPath()); |
|
|
|
|
|
|
|
string horrificDir = user.getPath() ~ "/.horrific"; |
|
|
|
try { |
|
|
|
mkdir(horrificDir); |
|
|
|
} catch (FileException e) { |
|
|
|
// only catch exception if EEXIST (dir already exists) |
|
|
|
if (e.errno != EEXIST) |
|
|
|
throw e; |
|
|
|
} |
|
|
|
|
|
|
|
RSAKeyPair keyPair = RSA.generateKeyPair(1024); |
|
|
|
write(horrificDir ~ "/id_rsa", keyPair.privateKey); |
|
|
|
write(horrificDir ~ "/id_rsa_pub", keyPair.publicKey); |
|
|
|
|
|
|
|
conf.users.insert(user.id(), user); |
|
|
|
conf.users.save(); |
|
|
|
return user.toJSONString(); |
|
|
|
} |