2020-11-22 13:49:34 +01:00
|
|
|
|
class AuthD::Request
|
2023-06-12 01:03:52 +02:00
|
|
|
|
IPC::JSON.message ReplaceProfile, 14 do
|
2020-11-22 13:49:34 +01:00
|
|
|
|
property new_profile : Hash(String, JSON::Any)
|
|
|
|
|
|
2023-06-12 01:03:52 +02:00
|
|
|
|
def initialize(@new_profile)
|
2020-11-22 13:49:34 +01:00
|
|
|
|
end
|
|
|
|
|
|
2023-06-10 17:26:12 +02:00
|
|
|
|
def handle(authd : AuthD::Service, fd : Int32)
|
2023-06-12 01:03:52 +02:00
|
|
|
|
logged_user = authd.get_logged_user? fd
|
|
|
|
|
return Response::Error.new "you must be logged" if logged_user.nil?
|
2020-11-22 13:49:34 +01:00
|
|
|
|
|
2023-06-12 01:03:52 +02:00
|
|
|
|
user = authd.user? logged_user.uid
|
|
|
|
|
return Response::Error.new "user not found" if user.nil?
|
2020-11-22 13:49:34 +01:00
|
|
|
|
|
|
|
|
|
profile = user.profile || Hash(String, JSON::Any).new
|
|
|
|
|
|
2023-06-12 01:03:52 +02:00
|
|
|
|
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
|
2020-11-22 13:49:34 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-12 01:03:52 +02:00
|
|
|
|
user.profile = @new_profile
|
2020-11-22 13:49:34 +01:00
|
|
|
|
|
|
|
|
|
authd.users_per_uid.update user.uid.to_s, user
|
|
|
|
|
|
|
|
|
|
Response::User.new user.to_public
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-06-12 01:03:52 +02:00
|
|
|
|
AuthD.requests << ReplaceProfile
|
2020-11-22 13:49:34 +01:00
|
|
|
|
|
|
|
|
|
# Same as above, but doesn’t reset the whole profile, only resets elements
|
|
|
|
|
# for which keys are present in `new_profile`.
|
2023-06-12 01:03:52 +02:00
|
|
|
|
IPC::JSON.message EditProfileEntries, 15 do
|
|
|
|
|
property user : UserID | Nil = nil
|
2020-11-22 13:49:34 +01:00
|
|
|
|
|
|
|
|
|
property new_profile : Hash(String, JSON::Any)
|
|
|
|
|
|
2023-06-12 01:03:52 +02:00
|
|
|
|
def initialize(@new_profile, @user = nil)
|
2020-11-22 13:49:34 +01:00
|
|
|
|
end
|
|
|
|
|
|
2023-06-10 17:26:12 +02:00
|
|
|
|
def handle(authd : AuthD::Service, fd : Int32)
|
2023-06-12 01:03:52 +02:00
|
|
|
|
logged_user = authd.get_logged_user? fd
|
|
|
|
|
return Response::Error.new "you must be logged" if logged_user.nil?
|
2020-11-22 13:49:34 +01:00
|
|
|
|
|
2023-06-12 01:03:52 +02:00
|
|
|
|
user = if u = @user
|
|
|
|
|
raise AdminAuthorizationException.new unless logged_user.admin
|
|
|
|
|
authd.user? u
|
2020-11-22 13:49:34 +01:00
|
|
|
|
else
|
2023-06-12 01:03:52 +02:00
|
|
|
|
authd.user? logged_user.uid
|
2020-11-22 13:49:34 +01:00
|
|
|
|
end
|
2023-06-12 01:03:52 +02:00
|
|
|
|
return Response::Error.new "user not found" if user.nil?
|
2020-11-22 13:49:34 +01:00
|
|
|
|
|
|
|
|
|
new_profile = user.profile || Hash(String, JSON::Any).new
|
|
|
|
|
|
2023-06-12 01:03:52 +02:00
|
|
|
|
unless logged_user.admin
|
2020-11-22 13:49:34 +01:00
|
|
|
|
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"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@new_profile.each do |key, value|
|
|
|
|
|
new_profile[key] = value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
user.profile = new_profile
|
|
|
|
|
|
|
|
|
|
authd.users_per_uid.update user.uid.to_s, user
|
|
|
|
|
|
|
|
|
|
Response::User.new user.to_public
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-06-12 01:03:52 +02:00
|
|
|
|
AuthD.requests << EditProfileEntries
|
2020-11-22 13:49:34 +01:00
|
|
|
|
end
|