PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/views/documentation/usage.php

https://bitbucket.org/skunkbad/community-auth
PHP | 149 lines | 137 code | 1 blank | 11 comment | 10 complexity | 201e2f4165c720865ebb100e3439aa97 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause
  1. <?php if( ! defined('BASEPATH') ) exit('No direct script access allowed');
  2. /**
  3. * Community Auth - Documentation of Usage View
  4. *
  5. * Community Auth is an open source authentication application for CodeIgniter 2.1.3
  6. *
  7. * @package Community Auth
  8. * @author Robert B Gottier
  9. * @copyright Copyright (c) 2011 - 2013, Robert B Gottier. (http://brianswebdesign.com/)
  10. * @license BSD - http://http://www.opensource.org/licenses/BSD-3-Clause
  11. * @link http://community-auth.com
  12. */
  13. ?>
  14. <h1>Documentation of Usage</h1>
  15. <ul class="std-list">
  16. <li><?php echo anchor('documentation/configuration', 'Configuration'); ?></li>
  17. <li><?php echo anchor('documentation/installation', 'Installation'); ?></li>
  18. <li><?php echo anchor('documentation/usage', 'Usage'); ?></li>
  19. </ul>
  20. <h2>Usage</h2>
  21. <p>
  22. Congratulations, if you've made it this far in the installation, you should be able to login and browse through the admin area of the example application. <?php echo anchor('documentation/login_debugging', 'Learn how to debug login if you cannot.'); ?> Since we both know that you have your own usage needs, you will need to know how to enforce authentication in your controllers, and how to detect who is who in views.
  23. </p>
  24. <h3>Enforcing Authentication by Role</h3>
  25. <p>
  26. This is probably the most useful, and easiest way to make sure a certain role is logged in. Check the example controllers, and you will see that inside a method that needs authentication, the entire contents of the method is wrapped inside an if statement like this:
  27. </p>
  28. <div class="doc_code">
  29. <pre class="brush: php; toolbar: false;">
  30. if( $this->require_role('admin,manager') )
  31. {
  32. // Method contents ...
  33. }</pre>
  34. </div>
  35. <p>
  36. If a user of an appropriate role is not logged in, the login form will automatically appear.
  37. </p>
  38. <h3>Enforcing Authentication by Role Group</h3>
  39. <div class="doc_code">
  40. <pre class="brush: php; toolbar: false;">
  41. if( $this->require_group('employees') )
  42. {
  43. // Method contents ...
  44. }</pre>
  45. </div>
  46. <p>
  47. If a user of an appropriate group is not logged in, the login form will automatically appear.
  48. </p>
  49. <h3>Enforcing Authentication by Account Level Number</h3>
  50. <p>
  51. If your user levels have been created in such a way that permissions are linear in nature, such as admin who can alter managers who can alter customers, and the admin is level 9, the managers are level 6, and the customers are level 1, then we can authenticate and allow access to the admin and managers by using the following inside the method of one of your controllers:
  52. </p>
  53. <div class="doc_code">
  54. <pre class="brush: php; toolbar: false;">
  55. if( $this->require_min_level(6) )
  56. {
  57. // Method contents ...
  58. }</pre>
  59. </div>
  60. <p>
  61. If a user of the appropriate level is not logged in, the login form will automatically appear.
  62. </p>
  63. <p>
  64. If you just want to make sure a user of any level is logged in:
  65. </p>
  66. <div class="doc_code">
  67. <pre class="brush: php; toolbar: false;">
  68. if( $this->require_min_level(1) )
  69. {
  70. // Do something ...
  71. }</pre>
  72. </div>
  73. <p>
  74. In this case, if a user of any level is not logged in, the login form will automatically appear.
  75. </p>
  76. <h3>Check if User Logged In</h3>
  77. <p>
  78. Most of the time, if you have a page that does not require login, but want to show a logout link or other information specific to a logged in user, you will use the following in the appropriate method of your controller:
  79. </p>
  80. <div class="doc_code">
  81. <pre class="brush: php; toolbar: false;">
  82. $this->is_logged_in();</pre>
  83. </div>
  84. <p>
  85. Calling is_logged_in() loads the variables shown below. Please note: the variables shown below will be set when enforcing authentication. You don't need to call is_logged_in() if you are already using require_role(), require_min_level(), etc.
  86. </p>
  87. <p>
  88. Also note: If you have set "cookie_secure" to TRUE in config/config, is_logged_in() will never return anything on a standard HTTP page. You can still see if somebody is logged in by testing for the <b>$_user_name, $_first_name or $_last_name</b> variables in your views. This allows for customization but <span style="color:red;">should not be used to authenticate the user</span>. As an example, the main template uses $_first_name.
  89. </p>
  90. <h3>Variables Accessible in Views</h3>
  91. <p>
  92. When a user is logged in, certain variables will be available to the views, because they are loaded by MY_Controller.
  93. </p>
  94. <ul class="std-list">
  95. <li><b>$auth_user_id</b> - The logged in user's user ID.</li>
  96. <li><b>$auth_user_name</b> - The logged in user's username.</li>
  97. <li><b>$auth_level</b> - The logged in user's account level by number.</li>
  98. <li><b>$auth_role</b> - The logged in user's account level by name.</li>
  99. <li><b>$auth_email</b> - The logged in user's email address.</li>
  100. </ul>
  101. <p>
  102. The following variables are set in the example application because the fields are represented in the selected profile columns array:
  103. </p>
  104. <ul class="std-list">
  105. <li><b>$auth_first_name</b> - The logged in user's first name.</li>
  106. <li><b>$auth_last_name</b> - The logged in user's last name.</li>
  107. </ul>
  108. <h3>Variables Accessible in Controller</h3>
  109. <p>
  110. When a user is logged in, the same variables that are available in views are set as CI_Controller class members. The exception would be the <b>auth_first_name</b> and <b>auth_last_name</b>. Class members are simply not set from the selected profile columns. This was preferred to using magic methods to get and set class members that aren't hard coded into MY_Controller, which is better for performance, and translates into better flexibility for the application because adding or subtracting fields from the selected profile columns only requires changes to the corresponding array in config/authentication.php.
  111. </p>
  112. <ul class="std-list">
  113. <li><b>$this->auth_user_id</b> - The logged in user's user ID.</li>
  114. <li><b>$this->auth_user_name</b> - The logged in user's username.</li>
  115. <li><b>$this->auth_level</b> - The logged in user's account level by number.</li>
  116. <li><b>$this->auth_role</b> - The logged in user's account level by name.</li>
  117. <li><b>$this->auth_email</b> - The logged in user's email address.</li>
  118. </ul>
  119. <h3>Variables Accessible as Config Items</h3>
  120. <p>
  121. When a user is logged in, the same variables that are available in views and controllers are available as config items. This is handy because they can be accessed in any model or library.
  122. </p>
  123. <ul class="std-list">
  124. <li><b>config_item('auth_user_id')</b> - The logged in user's user ID.</li>
  125. <li><b>config_item('auth_user_name')</b> - The logged in user's username.</li>
  126. <li><b>config_item('auth_level')</b> - The logged in user's account level by number.</li>
  127. <li><b>config_item('auth_role')</b> - The logged in user's account level by name.</li>
  128. <li><b>config_item('auth_email')</b> - The logged in user's email address.</li>
  129. </ul>
  130. <p>
  131. The following config items are set in the example application because the fields are represented in the selected profile columns array:
  132. </p>
  133. <ul class="std-list">
  134. <li><b>config_item('auth_first_name')</b> - The logged in user's first name.</li>
  135. <li><b>config_item('auth_last_name')</b> - The logged in user's last name.</li>
  136. </ul>
  137. <h3>More!</h3>
  138. <p>
  139. There are more ways to use Community Auth, and if you look at MY_Controller.php you will see some functions that you may or may not use. For now, I hope you will have learned enough to get you going. Usage really is quite simple. If you can't figure something out, please ask questions in the <a href="http://codeigniter.com/forums/" rel="external">CodeIgniter Forum</a>.
  140. </p>
  141. <p>
  142. For paid support, you may also contact me directly at <a href="http://brianswebdesign.com" rel="external">http://brianswebdesign.com</a>.
  143. </p>
  144. <?php
  145. /* End of file usage.php */
  146. /* Location: /application/views/documentation/usage.php */