indexing 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 is -- 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 (a_name, a_password, 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 is -- 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: O_USER_SESSION_MESSAGE do Result := "" -- argument validation -- some old users have old passwords which don't match the minimum length requirements. -- therefore we don't match the password against the validation regexp. if not is_valid_name (a_name) then last_fault := err_user_password_invalid else -- generate and send message create l_msg.make (a_name, 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 end end end login_authenticated (a_name: STRING): STRING is -- Login a user with a_name, return the session token on success. local l_msg: O_USER_LOGIN_AUTHENTICATED_MESSAGE l_session_msg: O_USER_SESSION_MESSAGE do Result := "" -- argument validation if not is_valid_name (a_name) then last_fault := err_user_password_invalid else -- generate and send message create l_msg.make (a_name) 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 end end end change_email (a_session: STRING; an_email_address: STRING): BOOLEAN_REF is -- 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 (a_session, 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 is -- Change enabled flag for the account local l_msg: O_USER_CHANGE_ACCOUNT_ENABLED_MESSAGE do -- argument validiation if not is_valid_name (a_user_name) then last_fault := err_invalid_user else -- generate and send message create l_msg.make (a_user_name, a_enabled) send_and_wait_for_reply (l_msg) if is_ok then create Result Result.set_item (True) end end end add_openid (a_username: STRING; an_openid: STRING): BOOLEAN_REF is -- Adds the openid to the user. require a_username_not_void: a_username /= Void an_openid_not_void: an_openid /= Void local l_msg: O_USER_ADD_OPENID_MESSAGE do -- argument validiation if not is_valid_name (a_username) then last_fault := err_invalid_name elseif not is_valid_url (an_openid) then last_fault := err_invalid_url else -- generate and send message create l_msg.make (a_username, an_openid) send_and_wait_for_reply (l_msg) if is_ok then create Result Result.set_item (True) end end end retrieve_user_from_openid (a_openid: STRING): STRING is -- Return email of user associated with a_username. local l_msg: O_USER_RETRIEVE_USER_FROM_OPENID_MESSAGE l_string_msg: O_GENERAL_STRING_MESSAGE do Result := "" -- argument validiation if not is_valid_url (a_openid) then last_fault := err_invalid_url else -- generate and send message create l_msg.make (a_openid) 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 end end end retrieve_password (a_username: STRING): STRING is -- Return password of user associated with a_username. local l_msg: O_USER_RETRIEVE_PASSWORD_MESSAGE l_string_msg: O_GENERAL_STRING_MESSAGE do Result := "" -- argument validiation if not is_valid_name (a_username) then last_fault := err_invalid_name else -- generate and send message create l_msg.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 end end end retrieve_email (a_username: STRING): STRING is -- Return email of user associated with a_username. local l_msg: O_USER_RETRIEVE_EMAIL_MESSAGE l_string_msg: O_GENERAL_STRING_MESSAGE do Result := "" -- argument validiation if not is_valid_name (a_username) then last_fault := err_invalid_name else -- generate and send message create l_msg.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 end end end retrieve_user_from_email (a_email: STRING): STRING is -- Return email of user associated with a_username. local l_msg: O_USER_RETRIEVE_USER_FROM_EMAIL_MESSAGE l_string_msg: O_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 (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 end end end change_password (a_username: STRING; a_password: STRING): BOOLEAN_REF is -- Change password of user username local l_msg: O_USER_CHANGE_PASSWORD_MESSAGE do -- argument validiation if not is_valid_name (a_username) 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 (a_username, 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 is -- Reset password of user a_name to a random value local l_msg: O_USER_RESET_PASSWORD_MESSAGE do -- argument validiation if not is_valid_name (a_name) then last_fault := err_invalid_name else -- generate and send message create l_msg.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 is -- 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 (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 end end end generate_key (a_session: STRING): BOOLEAN_REF is -- 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 (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 is -- 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 (add_openid_name) then create {TUPLE [STRING, STRING]}Result elseif a_name.is_equal (retrieve_user_from_openid_name) then create {TUPLE [STRING]}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 is -- 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 add_openid, add_openid_name, "Add Openid.") register_with_help (agent retrieve_user_from_openid, retrieve_user_from_openid_name, "Retrieve user from openid.") 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 is "add" login_name: STRING is "login" login_authenticated_name: STRING is "login_authenticated" add_openid_name: STRING is "add_openid" retrieve_user_from_openid_name: STRING is "retrieve_user_from_openid" change_email_name: STRING is "change_email" change_account_enabled_name: STRING is "change_account_enabled" retrieve_password_name: STRING is "retrieve_password" retrieve_email_name: STRING is "retrieve_email" retrieve_user_from_email_name: STRING is "retrieve_user_from_email" change_password_name: STRING is "change_password" reset_password_name: STRING is "reset_password" key_name: STRING is "key" generate_key_name: STRING is "generate_key" end