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 end
def edit_profile_content(user : Int32 | String, new_values) 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 response = AuthD.responses.parse_ipc_json read
case response case response

View File

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