92 lines
2.4 KiB
Crystal
92 lines
2.4 KiB
Crystal
class AuthD::Request
|
|
IPC::JSON.message AddUser, 1 do
|
|
property login : String
|
|
property password : String
|
|
property admin : Bool = false
|
|
property email : String? = nil
|
|
property profile : Hash(String, JSON::Any)? = nil
|
|
|
|
def initialize(@login, @password, @admin, @email, @profile)
|
|
end
|
|
|
|
def handle(authd : AuthD::Service, fd : Int32)
|
|
logged_user = authd.get_logged_user_full? fd
|
|
return Response::Error.new "you must be logged" if logged_user.nil?
|
|
|
|
logged_user.assert_permission("authd", "*", User::PermissionLevel::Admin)
|
|
|
|
if authd.users_per_login.get? @login
|
|
return Response::Error.new "login already used"
|
|
end
|
|
|
|
# No verification of the user's informations when an admin adds it.
|
|
# No mail address verification.
|
|
if authd.configuration.require_email && @email.nil?
|
|
return Response::Error.new "email required"
|
|
end
|
|
|
|
password_hash = authd.hash_password @password
|
|
|
|
uid = authd.new_uid
|
|
|
|
user = User.new uid, @login, password_hash
|
|
user.contact.email = @email unless @email.nil?
|
|
user.admin = @admin
|
|
|
|
@profile.try do |profile|
|
|
user.profile = profile
|
|
end
|
|
|
|
# We consider adding the user as a registration.
|
|
user.date_registration = Time.local
|
|
|
|
authd.users << user
|
|
authd.new_uid_commit uid
|
|
Response::UserAdded.new user.to_public
|
|
end
|
|
end
|
|
AuthD.requests << AddUser
|
|
|
|
IPC::JSON.message ModUser, 5 do
|
|
property user : UserID | Nil = nil
|
|
property admin : Bool? = nil
|
|
property password : String? = nil
|
|
property email : String? = nil
|
|
|
|
def initialize(@user, @admin, @password, @email)
|
|
end
|
|
|
|
def handle(authd : AuthD::Service, fd : Int32)
|
|
logged_user = authd.get_logged_user_full? fd
|
|
return Response::Error.new "you must be logged" if logged_user.nil?
|
|
|
|
user = if u = @user
|
|
logged_user.assert_permission("authd", "*", User::PermissionLevel::Edit)
|
|
authd.user? u
|
|
else
|
|
logged_user
|
|
end
|
|
return Response::Error.new "user not found" if user.nil?
|
|
|
|
# Only an admin can uprank or downrank someone.
|
|
if admin = @admin
|
|
logged_user.assert_permission("authd", "*", User::PermissionLevel::Admin)
|
|
user.admin = admin
|
|
end
|
|
|
|
@password.try do |s|
|
|
user.password_hash = authd.hash_password s
|
|
end
|
|
|
|
@email.try do |email|
|
|
user.contact.email = email
|
|
end
|
|
|
|
authd.users_per_uid.update user.uid.to_s, user
|
|
|
|
Response::UserEdited.new user.uid
|
|
end
|
|
end
|
|
AuthD.requests << ModUser
|
|
end
|