Simplification continues with the Profile class.

master
Philippe Pittoli 2023-06-12 01:03:52 +02:00
parent 5f3f208798
commit 0621d21ce0
2 changed files with 26 additions and 41 deletions

View File

@ -228,7 +228,7 @@ module AuthD
end
def edit_profile_content(user : Int32 | String, new_values)
send_now Request::EditProfileContent.new key, user, new_values
send_now Request::EditProfileEntries.new user, new_values
response = AuthD.responses.parse_ipc_json read
case response

View File

@ -1,76 +1,61 @@
class AuthD::Request
IPC::JSON.message EditProfile, 14 do
property token : String
IPC::JSON.message ReplaceProfile, 14 do
property new_profile : Hash(String, JSON::Any)
def initialize(@token, @new_profile)
def initialize(@new_profile)
end
def handle(authd : AuthD::Service, fd : Int32)
user = authd.get_user_from_token @token
logged_user = authd.get_logged_user? fd
return Response::Error.new "you must be logged" if logged_user.nil?
return Response::Error.new "invalid user" unless user
new_profile = @new_profile
user = authd.user? logged_user.uid
return Response::Error.new "user not found" if user.nil?
profile = user.profile || Hash(String, JSON::Any).new
authd.configuration.read_only_profile_keys.each do |key|
if new_profile[key]? != profile[key]?
return Response::Error.new "tried to edit read only key"
unless logged_user.admin
authd.configuration.read_only_profile_keys.each do |key|
if @new_profile[key]? != profile[key]?
return Response::Error.new "tried to edit read only key"
end
end
end
user.profile = new_profile
user.profile = @new_profile
authd.users_per_uid.update user.uid.to_s, user
Response::User.new user.to_public
end
end
AuthD.requests << EditProfile
AuthD.requests << ReplaceProfile
# Same as above, but doesnt reset the whole profile, only resets elements
# for which keys are present in `new_profile`.
IPC::JSON.message EditProfileContent, 15 do
property token : String? = nil
property shared_key : String? = nil
property user : Int32 | String | Nil
IPC::JSON.message EditProfileEntries, 15 do
property user : UserID | Nil = nil
property new_profile : Hash(String, JSON::Any)
def initialize(@shared_key, @user, @new_profile)
end
def initialize(@token, @new_profile)
def initialize(@new_profile, @user = nil)
end
def handle(authd : AuthD::Service, fd : Int32)
user = if token = @token
u = authd.get_user_from_token token
raise UserNotFound.new unless u
u
elsif shared_key = @shared_key
raise AdminAuthorizationException.new if shared_key != authd.configuration.shared_key
logged_user = authd.get_logged_user? fd
return Response::Error.new "you must be logged" if logged_user.nil?
u = @user
raise UserNotFound.new unless u
u = if u.is_a? Int32
authd.users_per_uid.get? u.to_s
else
authd.users_per_login.get? u
end
raise UserNotFound.new unless u
u
user = if u = @user
raise AdminAuthorizationException.new unless logged_user.admin
authd.user? u
else
raise AuthenticationInfoLacking.new
authd.user? logged_user.uid
end
return Response::Error.new "user not found" if user.nil?
new_profile = user.profile || Hash(String, JSON::Any).new
unless @shared_key
unless logged_user.admin
authd.configuration.read_only_profile_keys.each do |key|
if @new_profile.has_key? key
return Response::Error.new "tried to edit read only key"
@ -89,5 +74,5 @@ class AuthD::Request
Response::User.new user.to_public
end
end
AuthD.requests << EditProfileContent
AuthD.requests << EditProfileEntries
end