Module: HubSsoLib::Core
- Defined in:
- lib/hub_sso_lib.rb
Overview
Module: Core #
Various #
#
Purpose: The barely recognisable core of acts_as_authenticated’s #
AuthenticatedSystem module, modified to work with the #
other parts of HubSsoLib. You should include this module #
to use its facilities. #
#
Author: Various; adaptation by A.D.Hodgkinson #
#
History: 20-Oct-2006 (ADH): Integrated into HubSsoLib. #
Class Method Summary collapse
-
.included(base) ⇒ Object
Inclusion hook to make various methods available as ActionView helpers.
Instance Method Summary collapse
-
#hubssolib_account_link ⇒ Object
Returns markup for a link that leads to Hub’s conditional login endpoint, inline-styled as a red “Log in” or green “Account” button.
-
#hubssolib_afterwards ⇒ Object
Mandatory controller “after_action” callback method to tidy up after Hub actions during a request.
-
#hubssolib_authorized?(action = action_name, classname = self.class) ⇒ Boolean
Check if the user is authorized to perform the current action.
-
#hubssolib_beforehand ⇒ Object
Mandatory controller “before_action” callback method which activates HubSsoLib permissions management, session expiry and so-on.
- #hubssolib_clear_flash ⇒ Object
-
#hubssolib_current_user ⇒ Object
Accesses the current user, via the DRb server if necessary.
-
#hubssolib_current_user=(user) ⇒ Object
Sets the currently signed in user.
-
#hubssolib_destroy_user_sessions(hub_user_id) ⇒ Object
WARNING: Comparatively slow.
-
#hubssolib_ensure_https ⇒ Object
Ensure the current request is carried out over HTTPS by redirecting back to the current URL with the HTTPS protocol if it isn’t.
-
#hubssolib_enumerate_users ⇒ Object
WARNING: Slow.
-
#hubssolib_flash_data ⇒ Object
Return flash data for known keys, then all remaining keys, from both the cross-application and standard standard flash hashes.
-
#hubssolib_get_exception_message(id_data) ⇒ Object
Retrieve the message of an exception stored as an object in the given string.
-
#hubssolib_get_flash ⇒ Object
Flash data can be carried across the Hub session, stored in the DRb server as a result, and is thus cleared automatically if a session gets dropped.
-
#hubssolib_get_user_address ⇒ Object
Public read-only accessor methods for common user activities: return the current user’s e-mail address, or nil if there’s no user.
-
#hubssolib_get_user_id ⇒ Object
Public read-only accessor methods for common user activities: return the Hub database ID of the current user account, or nil if there’s no user.
-
#hubssolib_get_user_name ⇒ Object
Public read-only accessor methods for common user activities: return the current user’s name as a string, or nil if there’s no user.
-
#hubssolib_get_user_roles ⇒ Object
Public read-only accessor methods for common user activities: return the current user’s roles as a Roles object, or nil if there’s no user.
-
#hubssolib_log_in(user) ⇒ Object
Log in the user.
-
#hubssolib_log_out ⇒ Object
Log out the user.
-
#hubssolib_logged_in? ⇒ Boolean
Returns true or false if a user is logged in or not, respectively.
-
#hubssolib_privileged? ⇒ Boolean
Is the current user privileged? Anything other than normal user privileges will suffice.
-
#hubssolib_promote_uri_to_ssl(uri_str, host = nil) ⇒ Object
Take a URI and pass an optional host parameter.
-
#hubssolib_redirect_back_or_default(default) ⇒ Object
Redirect to the URI stored by the most recent store_location call or to the passed default.
-
#hubssolib_register_user_change_handler(app_name:, app_root:, task_name:) ⇒ Object
If an application needs to know about changes of a user e-mail address or display name (e.g. because of sync to a local relational store of users related to other application-managed resources, with therefore a desire to keep that store up to date), it can register a task to run on-change here.
-
#hubssolib_registered_user_change_handlers ⇒ Object
Returns all change handlers registered by prior calls made to #hubssolib_register_user_change_handler.
-
#hubssolib_retrieve_action(**args) ⇒ Object
The equivalent of #hubssolib_review_action, but for retrieving details of an action when Hub invokes your callback via POST request.
-
#hubssolib_retrieve_user_sessions(hub_user_id) ⇒ Object
WARNING: Comparatively slow.
-
#hubssolib_review_action(**args) ⇒ Object
Convenience accessor to HubSsoLib::Trust.get_trust_object().review_action - see that method for details.
- #hubssolib_set_flash(symbol, message) ⇒ Object
-
#hubssolib_store_location(uri_str = request.url) ⇒ Object
Store the URI of the current request in the session, or store the optional supplied specific URI.
-
#hubssolib_trusted? ⇒ Boolean
Convenience method that returns
trueif there’s a currently logged-in Hub user that has been flagged as trusted, else - whether there is no current user, or they haven’t been flagged as trusted - returnsfalse. -
#hubssolib_unique_name ⇒ Object
Return a human-readable unique ID for a user.
Class Method Details
.included(base) ⇒ Object
Inclusion hook to make various methods available as ActionView helpers.
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 |
# File 'lib/hub_sso_lib.rb', line 1544 def self.included(base) if base.respond_to?(:helper_method) base.send( :helper_method, :hubssolib_current_user, :hubssolib_unique_name, :hubssolib_account_link, :hubssolib_flash_data, :hubssolib_logged_in?, :hubssolib_authorized?, :hubssolib_privileged?, :hubssolib_trusted? ) end end |
Instance Method Details
#hubssolib_account_link ⇒ Object
Returns markup for a link that leads to Hub’s conditional login endpoint, inline-styled as a red “Log in” or green “Account” button. This can be used in page templates to avoid needing any additional images or other such resources and using pure HTML + CSS for the login indication.
JavaScript is used so that e.g. “back” button fully-cached displays by a browser will get updated with the correct login state, where needed (so long as the ‘pageshow’ event is supported). NOSCRIPT browsers use the old no-cache image fallback, which is much less efficient, but works.
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 |
# File 'lib/hub_sso_lib.rb', line 977 def hubssolib_account_link logged_in = self.hubssolib_logged_in?() ui_href = "#{HUB_PATH_PREFIX}/account/login_conditional" noscript_img_src = "#{HUB_PATH_PREFIX}/account/login_indication.png" noscript_img_tag = helpers.image_tag(noscript_img_src, size: '90x22', border: '0', alt: 'Log in or out') if self.respond_to?(:request) return_to_url = self.request.try(:original_url) if return_to_url.present? return_query = URI.encode_www_form({ return_to_url: return_to_url.to_s }) ui_href << "?#{return_query}" end end logged_in_link = helpers.link_to('Account', ui_href, id: 'hubssolib_logged_in_link') logged_out_link = helpers.link_to('Log in', ui_href, id: 'hubssolib_logged_out_link') noscript_link = helpers.link_to(noscript_img_tag, ui_href, id: 'hubssolib_login_noscript') # Yes, it's ugly, but yes, it works and it's a lot better for the server # to avoid the repeated image fetches. It probably works out as overall # more efficient for clients too - despite all the JS etc. work, there's # no network fetch overhead or image rendering. On mobile in particular, # the JS solution is likely to use less battery power. # safe_markup = " <div id=\"hubssolib_login_indication\">\n <noscript>\n \#{noscript_link}\n </noscript>\n </div>\n <script type=\"text/javascript\">\n const logged_in_html = \"\#{helpers.j(logged_in_link)}\";\n const logged_out_html = \"\#{helpers.j(logged_out_link)}\";\n const container = document.getElementById('hubssolib_login_indication')\n\n \#{\n # No '?.' support in NetSurf's JS engine, so can't do the match\n # and pop in a single line via \"?.pop() || ''\".\n }\n function hubSsoLibLoginStateWriteLink() {\n const regexp = '\#{helpers.j(HUB_LOGIN_INDICATOR_COOKIE)}\\\\s*=\\\\s*([^;]+)';\n const match = document.cookie.match(regexp);\n const flag = (match ? match.pop() : null) || '';\n\n if (flag === '\#{HUB_LOGIN_INDICATOR_COOKIE_VALUE}') {\n container.innerHTML = logged_in_html;\n } else {\n container.innerHTML = logged_out_html;\n }\n }\n \#{\n # Immediate update, plus on-load update - including fully cached\n # loads in the browser when the \"Back\" button is used. No stale\n # login indications should thus arise from cached data.\n }\n hubSsoLibLoginStateWriteLink();\n window.addEventListener('load', hubSsoLibLoginStateWriteLink);\n window.addEventListener('pageshow', hubSsoLibLoginStateWriteLink);\n </script>\n HTML\n\n return safe_markup.html_safe()\nend\n" |
#hubssolib_afterwards ⇒ Object
Mandatory controller “after_action” callback method to tidy up after Hub actions during a request. Usually invoked in ApplicationController.
1405 1406 1407 1408 1409 1410 1411 |
# File 'lib/hub_sso_lib.rb', line 1405 def hubssolib_afterwards begin DRb.current_server rescue DRb::DRbServerNotFound # Nothing to do; no service is running. end end |
#hubssolib_authorized?(action = action_name, classname = self.class) ⇒ Boolean
Check if the user is authorized to perform the current action. If calling from a helper, pass the action name and class name; otherwise by default, the current action name and ‘self.class’ will be used.
Override this method in your controllers if you want to restrict access to a different set of actions. Presently, the current user’s roles are compared against the caller’s permissions hash and the action name.
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 |
# File 'lib/hub_sso_lib.rb', line 1050 def (action = action_name, classname = self.class) # Classes with no permissions object always authorise everything. # Otherwise, ask the permissions object for its opinion. if (classname.respond_to? :) return classname..permitted?(hubssolib_get_user_roles, action) else return true end end |
#hubssolib_beforehand ⇒ Object
Mandatory controller “before_action” callback method which activates HubSsoLib permissions management, session expiry and so-on. Usually invoked in ApplicationController.
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 |
# File 'lib/hub_sso_lib.rb', line 1311 def hubssolib_beforehand # Does this action require a logged in user? # if (self.class.respond_to? :) login_is_required = !self.class..permitted?('', action_name) else login_is_required = false end # If we require login but we're logged out, redirect to Hub login. # NOTE EARLY EXIT # logged_in = hubssolib_logged_in? if logged_in == false .delete(HUB_LOGIN_INDICATOR_COOKIE, domain: :all, path: '/') if login_is_required hubssolib_store_location() return hubssolib_must_login() else return true end end [HUB_LOGIN_INDICATOR_COOKIE] = { value: HUB_LOGIN_INDICATOR_COOKIE_VALUE, path: '/', domain: :all, expires: 1.year, # I.e. *not* session-scope secure: ! hub_bypass_ssl?, httponly: false } # So we reach here knowing we're logged in, but the action may or # may not require authorisation. if (login_is_required) # Login *is* required for this action. If the session expires, # redirect to Hub's login page via its expiry action. Otherwise # check authorisation and allow action processing to continue # if OK, else indicate that access is denied. if (hubssolib_session_expired?) hubssolib_store_location() hubssolib_log_out() hubssolib_set_flash(:attention, 'Sorry, your session timed out; you need to log in again to continue.') # We mean this: redirect_to :controller => 'account', :action => 'login' # ...except for the Hub, rather than the current application (whatever # it may be). redirect_to HUB_PATH_PREFIX + '/account/login' else hubssolib_set_last_used(Time.now.utc) return ? true : hubssolib_access_denied() end else # We have to update session expiry even for actions that don't # need us to be logged in, since we *are* logged in and need to # maintain that state. If, though, the session expires, we just # quietly log out and let action processing carry on. if (hubssolib_session_expired?) hubssolib_log_out() hubssolib_set_flash(:attention, 'Your session timed out, so you are no longer logged in.') else hubssolib_set_last_used(Time.now.utc) end return true # true -> let action processing continue end rescue Exception => e Sentry.capture_exception(e) if defined?(Sentry) && Sentry.respond_to?(:capture_exception) # At this point there tends to be no Session data, so we're going to have # to encode the exception data into the URI... It must be escaped twice, # as many servers treat "%2F" in a URI as a "/". Apache can then fail to # serve the page, raising a 404 error unless "AllowEncodedSlashes on" is # specified in its configuration. # suffix = '/' + CGI::escape(CGI::escape(hubssolib_set_exception_data(e))) new_path = HUB_PATH_PREFIX + '/tasks/service' redirect_to(new_path + suffix) unless request.path.include?(new_path) end |
#hubssolib_clear_flash ⇒ Object
1483 1484 1485 1486 |
# File 'lib/hub_sso_lib.rb', line 1483 def hubssolib_clear_flash session = self.hubssolib_get_session() session.session_flash = {} unless session.nil? end |
#hubssolib_current_user ⇒ Object
Accesses the current user, via the DRb server if necessary. Returns a HubSsoLib::User object, or nil if none is available (this indicates that nobody is logged in, but #hubssolib_logged_in? should be used to check that, to allow for possible future more advanced logic within).
945 946 947 948 949 950 |
# File 'lib/hub_sso_lib.rb', line 945 def hubssolib_current_user hub_session = self.hubssolib_get_session() user = hub_session&.session_user return (user&.user_id.nil? ? nil : user) end |
#hubssolib_current_user=(user) ⇒ Object
Sets the currently signed in user. Note that although this works and is maintained, it is recommended that #hubssolib_log_in gets called instead.
user-
A valid HubSsoLib::User. This will replace any existing logged in user. If there is no session yet, one will be created.
958 959 960 961 962 963 964 965 |
# File 'lib/hub_sso_lib.rb', line 958 def hubssolib_current_user=(user) if user.nil? self.hubssolib_destroy_session! else hub_session = self.hubssolib_create_session() hub_session.session_user = user end end |
#hubssolib_destroy_user_sessions(hub_user_id) ⇒ Object
WARNING: Comparatively slow.
Remove all sessions under a given ID.
For information about performance limitations, see HubSsoLib::SessionFactory#destroy_sessions_by_user_id.
1216 1217 1218 |
# File 'lib/hub_sso_lib.rb', line 1216 def hubssolib_destroy_user_sessions(hub_user_id) hubssolib_factory().destroy_sessions_by_user_id(hub_user_id) unless hub_user_id.nil? end |
#hubssolib_ensure_https ⇒ Object
Ensure the current request is carried out over HTTPS by redirecting back to the current URL with the HTTPS protocol if it isn’t. Returns ‘true’ if not redirected (already HTTPS), else ‘false’.
1453 1454 1455 1456 1457 1458 1459 1460 1461 |
# File 'lib/hub_sso_lib.rb', line 1453 def hubssolib_ensure_https if request.ssl? || hub_bypass_ssl? return true else # This isn't reliable: redirect_to({ :protocol => 'https://' }) redirect_to( hubssolib_promote_uri_to_ssl( request.request_uri, request.host ) ) return false end end |
#hubssolib_enumerate_users ⇒ Object
WARNING: Slow.
Return an Array of HubSsoLib::User objects for all logged-in users, in an array. If a user is logged into more than one browser and thus has more than one session active, they will accordingly appear more than once in the returned data. This can also happen if a user loses key rotation and leaves an old, but not yet expired session behind when they log in anew.
In accordance with HubSsoLib::SessionFactory#enumerate_hub_session_ids documentation, a maximum of HUB_SESSION_ENUMERATION_KEY_MAX users can be returned here. If this is exceeded, an empty array is returned. If you are processing this information for display in a UI which itself requires a logged in user of some sort (which is very likely) and therefore know that at least one session does exist, you can treat an empty array as confirmation of “lots of sessions”. If you can’t be sure of at least one logged in user, then there is obvious arising ambiguity and this method does not solve it for you - it’s just “zero, or very many users”.
Users are ordered by least-recently-active first, most-recent last.
For information about performance limitations, see HubSsoLib::SessionFactory#enumerate_hub_session_ids.
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 |
# File 'lib/hub_sso_lib.rb', line 1169 def hubssolib_enumerate_users hub_session_info = hubssolib_factory().enumerate_hub_session_ids() hub_users = [] unless hub_session_info[:keys].nil? # (keyset too large, enumeration prohibited) hub_session_info[:keys].each do | key | session = hubssolib_factory().retrieve_session_by_key(key) hub_users << session.session_user unless session&.session_user&.user_id.nil? end end return hub_users end |
#hubssolib_flash_data ⇒ Object
Return flash data for known keys, then all remaining keys, from both the cross-application and standard standard flash hashes. The returned Hash is of the form:
{ 'hub' => ...data..., 'standard' => ...data... }
…where “…data…” is itself a Hash of flash keys yielding flash values. This allows both the Hub and standard flashes to have values inside them under the same key. All keys are strings.
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 |
# File 'lib/hub_sso_lib.rb', line 1498 def hubssolib_flash_data # These known key values are used to guarantee an order in the output # for cases where multiple messages are defined. # compiled_data = { 'hub' => {}, 'standard' => {} } ordered_keys = [ 'notice', 'attention', 'alert' ] # Get an array of keys for the Hub flash with the ordered key items # first and store data from that flash; same again for standard. hash = hubssolib_get_flash() keys = ordered_keys | hash.keys keys.each do | key | compiled_data['hub'][key] = hash[key] if hash.key?(key) end if defined?( flash ) hash = flash.to_h() keys = ordered_keys | hash.keys keys.each do | key | compiled_data['standard'][key] = hash[key] if hash.key?(key) end end hubssolib_clear_flash() flash.discard() return compiled_data end |
#hubssolib_get_exception_message(id_data) ⇒ Object
Retrieve the message of an exception stored as an object in the given string.
1538 1539 1540 |
# File 'lib/hub_sso_lib.rb', line 1538 def (id_data) hubssolib_get_exception_data(CGI::unescape(id_data)) end |
#hubssolib_get_flash ⇒ Object
Flash data can be carried across the Hub session, stored in the DRb server as a result, and is thus cleared automatically if a session gets dropped. However, we also want this to work without being logged in, so in that case it uses the normal flash as a backup when writing.
1468 1469 1470 1471 |
# File 'lib/hub_sso_lib.rb', line 1468 def hubssolib_get_flash session = self.hubssolib_get_session() session&.session_flash || {} end |
#hubssolib_get_user_address ⇒ Object
Public read-only accessor methods for common user activities: return the current user’s e-mail address, or nil if there’s no user.
1130 1131 1132 |
# File 'lib/hub_sso_lib.rb', line 1130 def hubssolib_get_user_address self.hubssolib_current_user&.user_email end |
#hubssolib_get_user_id ⇒ Object
Public read-only accessor methods for common user activities: return the Hub database ID of the current user account, or nil if there’s no user. See also hubssolib_unique_name.
1122 1123 1124 |
# File 'lib/hub_sso_lib.rb', line 1122 def hubssolib_get_user_id self.hubssolib_current_user&.user_id end |
#hubssolib_get_user_name ⇒ Object
Public read-only accessor methods for common user activities: return the current user’s name as a string, or nil if there’s no user. See also hubssolib_unique_name.
1114 1115 1116 |
# File 'lib/hub_sso_lib.rb', line 1114 def hubssolib_get_user_name self.hubssolib_current_user&.user_real_name end |
#hubssolib_get_user_roles ⇒ Object
Public read-only accessor methods for common user activities: return the current user’s roles as a Roles object, or nil if there’s no user.
1106 1107 1108 |
# File 'lib/hub_sso_lib.rb', line 1106 def hubssolib_get_user_roles self.hubssolib_current_user&.user_roles&.to_authenticated_roles end |
#hubssolib_log_in(user) ⇒ Object
Log in the user. This is just syntax sugar for setting the current user via #hubssolib_current_user, really. You can freely use either approach according to your preferred aesthetics, but this method is preferred.
user-
A valid HubSsoLib::User instance. If this has a
nilvalue foruser_id, or ifuseris itselfnil, you’ll cause the same effect as if explicitly logging out.
921 922 923 |
# File 'lib/hub_sso_lib.rb', line 921 def hubssolib_log_in(user) self.hubssolib_current_user = user # (which deals with all related session and cookie consequences) end |
#hubssolib_log_out ⇒ Object
Log out the user. Very few applications should ever need to call this, though Hub certainly does and it gets used internally too.
928 929 930 931 932 |
# File 'lib/hub_sso_lib.rb', line 928 def hubssolib_log_out self.hubssolib_current_user = nil # (which deals with all related session and cookie consequences) @hubssolib_session = nil .delete(HUB_LOGIN_INDICATOR_COOKIE, domain: :all, path: '/') end |
#hubssolib_logged_in? ⇒ Boolean
Returns true or false if a user is logged in or not, respectively.
936 937 938 |
# File 'lib/hub_sso_lib.rb', line 936 def hubssolib_logged_in? !!self.hubssolib_current_user end |
#hubssolib_privileged? ⇒ Boolean
Is the current user privileged? Anything other than normal user privileges will suffice. Can be called if not logged in. Returns ‘false’ for logged out or normal user privileges only, else ‘true’.
1066 1067 1068 1069 1070 1071 1072 1073 |
# File 'lib/hub_sso_lib.rb', line 1066 def hubssolib_privileged? return false unless hubssolib_logged_in? pnormal = HubSsoLib::Roles.new(false).to_s puser = hubssolib_get_user_roles().to_s return (puser && !puser.empty? && puser != pnormal) end |
#hubssolib_promote_uri_to_ssl(uri_str, host = nil) ⇒ Object
Take a URI and pass an optional host parameter. Decomposes the URI, sets the host you provide (or leaves it alone if you omit the parameter), then forces the scheme to ‘https’. Returns the result as a flat string.
1442 1443 1444 1445 1446 1447 |
# File 'lib/hub_sso_lib.rb', line 1442 def hubssolib_promote_uri_to_ssl(uri_str, host = nil) uri = URI.parse(uri_str) uri.host = host if host uri.scheme = hub_bypass_ssl? ? 'http' : 'https' return uri.to_s end |
#hubssolib_redirect_back_or_default(default) ⇒ Object
Redirect to the URI stored by the most recent store_location call or to the passed default.
1430 1431 1432 1433 1434 1435 |
# File 'lib/hub_sso_lib.rb', line 1430 def hubssolib_redirect_back_or_default(default) url = hubssolib_get_return_to() hubssolib_set_return_to(nil) redirect_to(url || default) end |
#hubssolib_register_user_change_handler(app_name:, app_root:, task_name:) ⇒ Object
If an application needs to know about changes of a user e-mail address or display name (e.g. because of sync to a local relational store of users related to other application-managed resources, with therefore a desire to keep that store up to date), it can register a task to run on-change here. When a user edits their information, Hub runs through all such commands, allowing external applications to manage their own state with no need for coupled configuration or other duplication.
The registered name must be a Rake task and the application must specify its location in the filesystem so that the PWD can be changed there, in order to execute the Rake task via “bundle exec”. The task is passed the following parameters, in the specified order:
-
User’s old e-mail address
-
User’s old unique display name (as returned by #hubssolib_unique_name)
-
User’s new e-mail address (which might be the same as the old)
-
User’s old unique display name (which might be unchanged too)
This is a newer Hub interface which uses named parameters rather than positional:
app_name-
Application name, e.g. “beast”; make sure this is unique.
app_root-
Application’s Rails root, e.g. “/home/fred/rails/beast”.
task_name-
Rake task name, e.g. “hub:update_user”.
An example invocation in “config/application.rb” might look like this:
module Foo
class Application < Rails::Application
require HubSsoLib::Core
hubssolib_register_user_change_handler(
app_name: Rails.application.name,
app_root: Rails.root,
task_name: 'hub:update_user'
)
config.load_defaults 8.0 # ...etc...
end
end
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 |
# File 'lib/hub_sso_lib.rb', line 1261 def hubssolib_register_user_change_handler(app_name:, app_root:, task_name:) File.open(HUB_COMMAND_REGISTRY, File::RDWR | File::CREAT) do |file| file.flock(File::LOCK_EX) commands_json = file.read() commands_hash = (JSON.parse(commands_json) rescue nil) if commands_json.present? commands_hash ||= {} file.rewind() commands_hash[app_name] = { root: app_root, task: task_name } file.write(JSON.fast_generate(commands_hash)) file.truncate(file.pos) end end |
#hubssolib_registered_user_change_handlers ⇒ Object
Returns all change handlers registered by prior calls made to #hubssolib_register_user_change_handler. Returns a Hash, keyed by Rails application name, with values of another Hash:
-
root=> Rails application root -
task=> Name of Rake task to be run
All keys are Strings.
This is usually called by the Hub application only, when it is processing a user’s request to change their information.
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 |
# File 'lib/hub_sso_lib.rb', line 1293 def hubssolib_registered_user_change_handlers commands_hash = {} File.open(HUB_COMMAND_REGISTRY, File::RDWR | File::CREAT) do |file| file.flock(File::LOCK_EX) commands_json = file.read() commands_hash = (JSON.parse(commands_json) rescue nil) if commands_json.present? commands_hash ||= {} end return commands_hash end |
#hubssolib_retrieve_action(**args) ⇒ Object
The equivalent of #hubssolib_review_action, but for retrieving details of an action when Hub invokes your callback via POST request.
1098 1099 1100 |
# File 'lib/hub_sso_lib.rb', line 1098 def hubssolib_retrieve_action(**args) HubSsoLib::Trust.get_trust_object().retrieve_action(**args) end |
#hubssolib_retrieve_user_sessions(hub_user_id) ⇒ Object
WARNING: Comparatively slow.
For a given HubSsoLib::User’s user ID, return any known sessions held by the DRb server, as an Hash of session keys with HubSsoLib::Session instances as values.
Returns an empty Hash if the given ID is nil.
Note that Hub sessions can disappear at any moment, so the session keys you find in the Hash might refer to extinct sessions by the time you get to do something with them. You can still access the data, but if you were to try and ask the DRb server for that key, it’d return nil.
Sessions are ordered by least-recently-active first, most-recent last.
For information about performance limitations, see HubSsoLib::SessionFactory#retrieve_sessions_by_user_id.
1201 1202 1203 1204 1205 1206 1207 |
# File 'lib/hub_sso_lib.rb', line 1201 def hubssolib_retrieve_user_sessions(hub_user_id) if hub_user_id.nil? {} else hubssolib_factory().retrieve_sessions_by_user_id(hub_user_id) end end |
#hubssolib_review_action(**args) ⇒ Object
Convenience accessor to HubSsoLib::Trust.get_trust_object().review_action
-
see that method for details. Note that the Trust object is implemented
in the Hub application, not here; see:
HubSsoLib::Trust::Server::review_action
…in “app/hub/lib/hub_sso_lib/trust/server.rb”.
1091 1092 1093 |
# File 'lib/hub_sso_lib.rb', line 1091 def hubssolib_review_action(**args) HubSsoLib::Trust.get_trust_object().review_action(**args) end |
#hubssolib_set_flash(symbol, message) ⇒ Object
1473 1474 1475 1476 1477 1478 1479 1480 1481 |
# File 'lib/hub_sso_lib.rb', line 1473 def hubssolib_set_flash(symbol, ) session = self.hubssolib_get_session() f = hubssolib_get_flash() unless session.nil? f = self.flash if f.nil? && self.respond_to?(:flash) f[symbol] = session.session_flash = f unless session.nil? end |
#hubssolib_store_location(uri_str = request.url) ⇒ Object
Store the URI of the current request in the session, or store the optional supplied specific URI.
We can return to this location by calling #redirect_back_or_default.
1418 1419 1420 1421 1422 1423 1424 1425 |
# File 'lib/hub_sso_lib.rb', line 1418 def hubssolib_store_location(uri_str = request.url) if (uri_str && !uri_str.empty?) uri_str = hubssolib_promote_uri_to_ssl(uri_str, request.host) unless request.ssl? hubssolib_set_return_to(uri_str) else hubssolib_set_return_to(nil) end end |
#hubssolib_trusted? ⇒ Boolean
Convenience method that returns true if there’s a currently logged-in Hub user that has been flagged as trusted, else - whether there is no current user, or they haven’t been flagged as trusted - returns false.
1079 1080 1081 |
# File 'lib/hub_sso_lib.rb', line 1079 def hubssolib_trusted? self.hubssolib_current_user&.user_trusted == 'true' end |
#hubssolib_unique_name ⇒ Object
Return a human-readable unique ID for a user. We don’t want to have e-mail addresses all over the place, but don’t want to rely on real names as unique - they aren’t. Instead, produce a composite of the user’s account database ID (which must be unique by definition) and their real name. See also hubssolib_get_name.
1141 1142 1143 1144 |
# File 'lib/hub_sso_lib.rb', line 1141 def hubssolib_unique_name user = self.hubssolib_current_user user ? "#{user.user_real_name} (#{user.user_id})" : 'Anonymous' end |