note description: "API interface to internal_user services." author: "Patrick Ruckstuhl " date: "$Date$" revision: "$Revision$" class INTERNAL_USER_SERVICE inherit API_SERVICE create make feature -- Basic operations add (a_name, a_password, a_email: STRING): BOOLEAN_REF -- Create a new user with a_name, a_password and a_email local l_msg: O_USER_CREATE_MESSAGE do -- argument validiation if not is_valid_name (a_name) then last_fault := err_invalid_name elseif not is_valid_password (a_password) then last_fault := err_invalid_password elseif not is_valid_email (a_email) then last_fault := err_invalid_email else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_name), create {A_STRING_VALUE}.make (a_password), create {A_STRING_VALUE}.make (a_email)) send_and_wait_for_reply (l_msg) if is_ok then create Result Result.set_item (True) end end end login (a_name, a_password: STRING): STRING -- Login a user with a_name and a_password, return the session token on success. local l_msg: O_USER_LOGIN_MESSAGE l_session_msg: A_SESSION_MESSAGE do Result := "" -- argument validation -- some old users have old passwords/usernames which don't match the requirements. -- therefore we don't match them against the validation regexp. if a_name = Void or else a_name.is_empty or a_password = Void or else a_password.is_empty then last_fault := err_user_password_invalid else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_name), create {A_STRING_VALUE}.make (a_password)) send_and_wait_for_reply (l_msg) if is_ok then l_session_msg ?= last_reply check session_message: l_session_msg /= Void end Result := l_session_msg.session.value end end end login_authenticated (a_username: STRING): STRING -- Login a user with a_name, return the session token on success. local l_msg: O_USER_LOGIN_AUTHENTICATED_MESSAGE l_session_msg: A_SESSION_MESSAGE do Result := "" -- argument validation if a_username.is_empty then last_fault := err_user_password_invalid else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_username)) send_and_wait_for_reply (l_msg) if is_ok then l_session_msg ?= last_reply check session_message: l_session_msg /= Void end Result := l_session_msg.session.value end end end change_email (a_session: STRING; an_email_address: STRING): BOOLEAN_REF -- Change email address of user with session a_session local l_msg: O_USER_CHANGE_EMAIL_MESSAGE do -- argument validiation check_anonymous_session (a_session) if not is_valid_session (a_session) then last_fault := err_invalid_session elseif not is_valid_email (an_email_address) then last_fault := err_invalid_email else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_session), create {A_STRING_VALUE}.make (an_email_address)) send_and_wait_for_reply (l_msg) if is_ok then create Result Result.set_item (True) end end end change_account_enabled (a_user_name: STRING; a_enabled: BOOLEAN_REF): BOOLEAN_REF -- Change enabled flag for the account local l_msg: O_USER_CHANGE_ACCOUNT_ENABLED_MESSAGE do -- argument validiation if a_user_name.is_empty then last_fault := err_invalid_user else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_user_name), create {A_BOOLEAN_VALUE}.make (a_enabled.item)) send_and_wait_for_reply (l_msg) if is_ok then create Result Result.set_item (True) end end end retrieve_password (a_username: STRING): STRING -- Return password of user associated with a_username. local l_msg: O_USER_RETRIEVE_PASSWORD_MESSAGE l_string_msg: A_GENERAL_STRING_MESSAGE do Result := "" -- argument validiation if a_username.is_empty then last_fault := err_invalid_name else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_username)) send_and_wait_for_reply (l_msg) if is_ok then l_string_msg ?= last_reply check string_message: l_string_msg /= Void end Result := l_string_msg.string.value end end end retrieve_email (a_username: STRING): STRING -- Return email of user associated with a_username. local l_msg: O_USER_RETRIEVE_EMAIL_MESSAGE l_string_msg: A_GENERAL_STRING_MESSAGE do Result := "" -- argument validiation if a_username.is_empty then last_fault := err_invalid_name else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_username)) send_and_wait_for_reply (l_msg) if is_ok then l_string_msg ?= last_reply check string_message: l_string_msg /= Void end Result := l_string_msg.string.value end end end retrieve_user_from_email (a_email: STRING): STRING -- Return email of user associated with a_username. local l_msg: O_USER_RETRIEVE_USER_FROM_EMAIL_MESSAGE l_string_msg: A_GENERAL_STRING_MESSAGE do Result := "" -- argument validiation if not is_valid_email (a_email) then last_fault := err_invalid_email else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_email)) send_and_wait_for_reply (l_msg) if is_ok then l_string_msg ?= last_reply check string_message: l_string_msg /= Void end Result := l_string_msg.string.value end end end change_password (a_username: STRING; a_password: STRING): BOOLEAN_REF -- Change password of user username local l_msg: O_USER_CHANGE_PASSWORD_MESSAGE do -- argument validiation if a_username.is_empty then last_fault := err_invalid_name elseif not is_valid_password (a_password) then last_fault := err_invalid_password else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_username), create {A_STRING_VALUE}.make (a_password)) send_and_wait_for_reply (l_msg) if is_ok then create Result Result.set_item (True) end end end reset_password (a_name: STRING): BOOLEAN_REF -- Reset password of user a_name to a random value local l_msg: O_USER_RESET_PASSWORD_MESSAGE do -- argument validiation if a_name.is_empty then last_fault := err_invalid_name else -- generate and send message create l_msg.make (create {A_STRING_VALUE}.make (a_name)) send_and_wait_for_reply (l_msg) if is_ok then create Result Result.set_item (True) end end end key (a_session: STRING): STRING -- Retrieve the user key (or a fault if it has not yet been generated). local l_msg: O_USER_KEY_MESSAGE l_reply: O_USER_KEY_REPLY_MESSAGE do -- argument validiation check_anonymous_session (a_session) if not is_valid_session (a_session) then last_fault := err_invalid_session else create l_msg.make (create {A_STRING_VALUE}.make (a_session)) send_and_wait_for_reply (l_msg) if is_ok then l_reply ?= last_reply check correct_msg_type: l_reply /= Void end Result := l_reply.key.value end end end generate_key (a_session: STRING): BOOLEAN_REF -- Generate a new user key. local l_msg: O_USER_KEY_GENERATE_MESSAGE do -- argument validiation check_anonymous_session (a_session) if not is_valid_session (a_session) then last_fault := err_invalid_session else create l_msg.make (create {A_STRING_VALUE}.make (a_session)) send_and_wait_for_reply (l_msg) if is_ok then create Result Result.set_item (True) end end end feature -- Creation new_tuple (a_name: STRING): TUPLE -- Tuple of default-valued arguments to pass to call `a_name'. do if a_name.is_equal (add_name) then create {TUPLE [STRING, STRING, STRING]}Result elseif a_name.is_equal (login_name) then create {TUPLE [STRING, STRING]}Result elseif a_name.is_equal (login_authenticated_name) then create {TUPLE [STRING]}Result elseif a_name.is_equal (change_email_name) then create {TUPLE [STRING, STRING]}Result elseif a_name.is_equal (change_account_enabled_name) then create {TUPLE [STRING, BOOLEAN_REF]}Result elseif a_name.is_equal (retrieve_password_name) then create {TUPLE [STRING]}Result elseif a_name.is_equal (retrieve_email_name) then create {TUPLE [STRING]}Result elseif a_name.is_equal (retrieve_user_from_email_name) then create {TUPLE [STRING]}Result elseif a_name.is_equal (change_password_name) then create {TUPLE [STRING, STRING]}Result elseif a_name.is_equal (reset_password_name) then create {TUPLE [STRING]}Result elseif a_name.is_equal (key_name) then create {TUPLE [STRING]}Result elseif a_name.is_equal (generate_key_name) then create {TUPLE [STRING]}Result end end feature -- Initialisation self_register -- Register all actions for this service do register_with_help (agent add, add_name, "Create a new user account.") register_with_help (agent login, login_name, "Login with name and password and receive a session id.") register_with_help (agent login_authenticated, login_authenticated_name, "Login with name and receive a session id.") register_with_help (agent change_email, change_email_name, "Change email address.") register_with_help (agent change_account_enabled, change_account_enabled_name, "Change enabled flag.") register_with_help (agent retrieve_password, retrieve_password_name, "Users password.") register_with_help (agent retrieve_email, retrieve_email_name, "Users email address.") register_with_help (agent retrieve_user_from_email, retrieve_user_from_email_name, "Get user from email address.") register_with_help (agent change_password, change_password_name, "Change password.") register_with_help (agent reset_password, reset_password_name, "Reset password to a random password and mail the new password.") register_with_help (agent key, key_name, "Retrieve user key for login per key.") register_with_help (agent generate_key, generate_key_name, "Generate a new user key.") end feature {NONE} -- Implementation add_name: STRING = "add" login_name: STRING = "login" login_authenticated_name: STRING = "login_authenticated" change_email_name: STRING = "change_email" change_account_enabled_name: STRING = "change_account_enabled" retrieve_password_name: STRING = "retrieve_password" retrieve_email_name: STRING = "retrieve_email" retrieve_user_from_email_name: STRING = "retrieve_user_from_email" change_password_name: STRING = "change_password" reset_password_name: STRING = "reset_password" key_name: STRING = "key" generate_key_name: STRING = "generate_key" end