DODB API updates.

ipc07
Luka Vandervelden 2020-01-04 08:40:13 +01:00
parent 60b1ca6bc7
commit e1c204b742
1 changed files with 13 additions and 11 deletions

View File

@ -14,9 +14,11 @@ class AuthD::Service
property registrations_allowed = false property registrations_allowed = false
@users_per_login : DODB::Index(User) @users_per_login : DODB::Index(User)
@users_per_uid : DODB::Index(User)
def initialize(@storage_root : String, @jwt_key : String) def initialize(@storage_root : String, @jwt_key : String)
@users = DODB::DataBase(String, User).new @storage_root @users = DODB::DataBase(User).new @storage_root
@users_per_uid = @users.new_index "uid", &.uid.to_s
@users_per_login = @users.new_index "login", &.login @users_per_login = @users.new_index "login", &.login
@last_uid_file = "#{@storage_root}/last_used_uid" @last_uid_file = "#{@storage_root}/last_used_uid"
@ -77,7 +79,7 @@ class AuthD::Service
user.profile = profile user.profile = profile
end end
@users[user.uid.to_s] = user @users << user
Response::UserAdded.new user.to_public Response::UserAdded.new user.to_public
when Request::GetUserByCredentials when Request::GetUserByCredentials
@ -95,7 +97,7 @@ class AuthD::Service
when Request::GetUser when Request::GetUser
uid_or_login = request.user uid_or_login = request.user
user = if uid_or_login.is_a? Int32 user = if uid_or_login.is_a? Int32
@users[uid_or_login.to_s]? @users_per_uid.get? uid_or_login.to_s
else else
@users_per_login.get? uid_or_login @users_per_login.get? uid_or_login
end end
@ -110,7 +112,7 @@ class AuthD::Service
return Response::Error.new "invalid authentication key" return Response::Error.new "invalid authentication key"
end end
user = @users[request.uid.to_s]? user = @users_per_uid.get? request.uid.to_s
unless user unless user
return Response::Error.new "user not found" return Response::Error.new "user not found"
@ -120,7 +122,7 @@ class AuthD::Service
user.password_hash = hash_password s user.password_hash = hash_password s
end end
@users[user.uid.to_s] = user @users_per_uid.update user.uid.to_s, user
Response::UserEdited.new request.uid Response::UserEdited.new request.uid
when Request::Register when Request::Register
@ -141,7 +143,7 @@ class AuthD::Service
user.profile = profile user.profile = profile
end end
@users[user.uid.to_s] = user @users_per_uid.update user.uid.to_s, user
Response::UserAdded.new user.to_public Response::UserAdded.new user.to_public
when Request::UpdatePassword when Request::UpdatePassword
@ -157,7 +159,7 @@ class AuthD::Service
user.password_hash = hash_password request.new_password user.password_hash = hash_password request.new_password
@users[user.uid.to_s] = user @users_per_uid.update user.uid.to_s, user
Response::UserEdited.new user.uid Response::UserEdited.new user.uid
when Request::ListUsers when Request::ListUsers
@ -182,7 +184,7 @@ class AuthD::Service
return Response::Error.new "unauthorized" return Response::Error.new "unauthorized"
end end
user = @users[request.user.to_s]? user = @users_per_uid.get? request.user.to_s
if user.nil? if user.nil?
return Response::Error.new "no such user" return Response::Error.new "no such user"
@ -207,7 +209,7 @@ class AuthD::Service
return Response::Error.new "unauthorized" return Response::Error.new "unauthorized"
end end
user = @users[request.user.to_s]? user = @users_per_uid.get? request.user.to_s
if user.nil? if user.nil?
return Response::Error.new "no such user" return Response::Error.new "no such user"
@ -227,7 +229,7 @@ class AuthD::Service
service_permissions[request.resource] = request.permission service_permissions[request.resource] = request.permission
end end
@users[user.uid.to_s] = user @users_per_uid.update user.uid.to_s, user
Response::PermissionSet.new user.uid, service, request.resource, request.permission Response::PermissionSet.new user.uid, service, request.resource, request.permission
else else
@ -238,7 +240,7 @@ class AuthD::Service
def get_user_from_token(token : String) def get_user_from_token(token : String)
token_payload = Token.from_s(token, @jwt_key) token_payload = Token.from_s(token, @jwt_key)
@users[token_payload.uid.to_s]? @users_per_uid.get? token_payload.uid.to_s
end end
def run def run