SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/UserPerms.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\UserPerms' class from the namespace
  24. * registered for the feature "users".
  25. **/
  26. class UserPerms
  27. {
  28. /**
  29.   * Cached instance across multiple calls. FALSE = not yet loaded
  30.   */
  31. public static $user_perms_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 realUserPermsInst()
  41. {
  42. if (self::$user_perms_inst !== false) {
  43. return self::$user_perms_inst;
  44. }
  45.  
  46. if (Register::hasFeature('users')) {
  47. $ns = Register::getFeatureNamespace('users');
  48. $class = $ns . '\Helpers\UserPerms';
  49. self::$user_perms_inst = Sprout::instance($class);
  50. } else {
  51. self::$user_perms_inst = null;
  52. }
  53.  
  54. return self::$user_perms_inst;
  55. }
  56.  
  57.  
  58. /**
  59.   * Stub method for when the users module is not installed
  60.   * See {@see SproutModules\Karmabunny\Users\Helpers\UserPerms::checkPermissionsTree}
  61.   * @return bool True if the user has access, false otherwise
  62.   */
  63. public static function checkPermissionsTree($table, $id)
  64. {
  65. $inst = self::realUserPermsInst();
  66. if ($inst) {
  67. return $inst->checkPermissionsTree($table, $id);
  68. } else {
  69. return true;
  70. }
  71. }
  72.  
  73. /**
  74.   * Stub method; uses real one if Users module is installed.
  75.   * See {@see SproutModules\Karmabunny\Users\Helpers\UserPerms::getAccessableGroups}
  76.   * @return array Each element is a category id
  77.   */
  78. public static function getAccessableGroups($table, $id)
  79. {
  80. $inst = self::realUserPermsInst();
  81. if ($inst) {
  82. return $inst->getAccessableGroups($table, $id);
  83. } else {
  84. return [];
  85. }
  86. }
  87.  
  88. /**
  89.   * Stub method; uses real one if Users module is installed.
  90.   * See {@see SproutModules\Karmabunny\Users\Helpers\UserPerms::getAccessDenied}
  91.   * @return View
  92.   */
  93. public static function getAccessDenied()
  94. {
  95. $inst = self::realUserPermsInst();
  96. if ($inst) {
  97. return $inst->getAccessDenied();
  98. } else {
  99. return null;
  100. }
  101. }
  102.  
  103. /**
  104.   * Stub method; uses real one if Users module is installed.
  105.   * See {@see SproutModules\Karmabunny\Users\Helpers\UserPerms::getAllCategories}
  106.   * @return array id => name
  107.   */
  108. public static function getAllCategories()
  109. {
  110. $inst = self::realUserPermsInst();
  111. if ($inst) {
  112. return $inst->getAllCategories();
  113. } else {
  114. return [];
  115. }
  116. }
  117. }
  118.  
  119.  
  120.