SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/UserAuth.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16.  
  17. /**
  18. * Stub class for when the users module is not installed
  19. *
  20. * Makes use of the fact that it's legal to call static
  21. * methods from instances
  22. *
  23. * Will load the 'Helpers\UserAuth' class from the namespace
  24. * registered for the feature "users".
  25. **/
  26. class UserAuth
  27. {
  28. /**
  29.   * Cached instance across multiple calls. FALSE = not yet loaded
  30.   */
  31. public static $user_auth_inst = false;
  32.  
  33.  
  34. /**
  35.   * Create an instance of the "real" user perms class, if it's available.
  36.   *
  37.   * @return object The "real" user perms class
  38.   * @return null No module registering the feature 'users' is loaded
  39.   */
  40. protected static function realUserAuthInst()
  41. {
  42. if (self::$user_auth_inst !== false) {
  43. return self::$user_auth_inst;
  44. }
  45.  
  46. if (Register::hasFeature('users')) {
  47. $ns = Register::getFeatureNamespace('users');
  48. $class = $ns . '\Helpers\UserAuth';
  49. self::$user_auth_inst = Sprout::instance($class);
  50. } else {
  51. self::$user_auth_inst = null;
  52. }
  53.  
  54. return self::$user_auth_inst;
  55. }
  56.  
  57.  
  58. /**
  59.   * Stub method for when the users module is not installed
  60.   * See {@see SproutModules\Karmabunny\Users\Helpers\UserAuth::isLoggedIn}
  61.   * @return bool True if the user is logged in, false otherwise
  62.   */
  63. public static function isLoggedIn()
  64. {
  65. $inst = self::realUserAuthInst();
  66. if ($inst) {
  67. return $inst->isLoggedIn();
  68. } else {
  69. return false;
  70. }
  71. }
  72.  
  73.  
  74. /**
  75.   * Gets id of logged-in user
  76.   * See {@see SproutModules\Karmabunny\Users\Helpers\UserAuth::getId}
  77.   * @return int 0 if user isn't logged in
  78.   */
  79. public static function getId()
  80. {
  81. $inst = self::realUserAuthInst();
  82. if ($inst) {
  83. return $inst->getId();
  84. } else {
  85. return 0;
  86. }
  87. }
  88. }
  89.