PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/README.markdown

https://github.com/DavertMik/doAuthPlugin
Markdown | 276 lines | 205 code | 71 blank | 0 comment | 0 complexity | 95843b9166ec2589cb7726088ce9b4b4 MD5 | raw file
  1. doAuthPlugin
  2. -------------
  3. Inspired by sfGuardPlugin an easy-to-use new authorization system. doAuth is ready to work out of the box with just a few configuration changes. It takes all the common user functionality - User authorization by login and password, registration, activation by email, password reset. If you don't need sfGuard permissions system but a simple user module that works, you should try doAuth. Great for a new symfony 1.4 projects.
  4. Features
  5. --------
  6. * All common user actions: authorization, registration, activation, password reset works out the box.
  7. * Developer friendly: only 2 tables, model named 'User' that can be extended with Doctrine inheritance.
  8. * Highly configurable and customizable. You can extend classes, add your event handlers or just edit configuration to create custom behavior.
  9. * Standard emails are sent on registration, activation and password requests.
  10. * Refactored from sfGuard. Shares similar concepts and some config APIs.
  11. Coming Soon: doAccess plugin that adds a permissions functionality on top of doAuth plugin.
  12. Installation
  13. ------------
  14. * Install the plugin
  15. $ symfony plugin:install doAuthPlugin
  16. * Build your model and forms:
  17. $ symfony doctrine:build-model
  18. $ symfony doctrine:build-forms
  19. * Update your database:
  20. $ symfony doctrine:insert-sql
  21. * make myUser class typically located in app/frontend/lib to extend doAuthSecurityUser:
  22. [php]
  23. class myUser extends doAuthSecurityUser
  24. {
  25. }
  26. * Optionally add the "Remember Me" filter to `filters.yml` above the security filter:
  27. remember_me:
  28. class: doAuthRememberMeFilter
  29. * Enable [baseAuth] module in setting.yml of your frontend or skip it and start the customization.
  30. * Change the default login and secure modules in `settings.yml`
  31. login_module: baseAuth
  32. login_action: signin
  33. secure_module: baseAuth
  34. secure_action: secure
  35. You are ready to use. Try to access /register, /login, /logout routes.
  36. By default doAuth automaticaly signs user is on registration and sends email with username and password.
  37. Usage
  38. -----
  39. Access your user model from a class User.
  40. (well, is very common thing, but should be noted for sfGuardUser users)
  41. * creating a user (example)
  42. [php]
  43. $user = new User();
  44. $user->setUsername('davert');
  45. $user->setPassword('symfony');
  46. $user->setEmail('doAuth843@davert.mail.ua');
  47. $user->save();
  48. * accessing user session class (example in controller)
  49. [php]
  50. $user = $this->getUser();
  51. // retrieve current user object
  52. $user->getAccount();
  53. // get user Id
  54. $user->getUserId();
  55. // check if user is admin or superadmin
  56. $user->isAdmin();
  57. Customization
  58. -------------
  59. * Extend the User model if you need to, in your schema.yml. Currently user model contains 'username', 'email', 'last_login', 'is_active', 'is_super_admin' fields and hashed password.
  60. * Don't use baseAuth module. Create your own User module:
  61. $ symfony generate:module frontend user
  62. * let userActions extend the doAuthActions class.
  63. [php]
  64. class userActions extends doAuthActions
  65. * userActions now implements common actions: signin, signout, register, activate, reset password.
  66. * disable [baseAuth] module in settings.yml if it is enabled.
  67. * activate the standard routes in your frontendConfiguration class:
  68. [php]
  69. class frontendConfiguration extends sfApplicationConfiguration
  70. {
  71. public function configure()
  72. {
  73. $this->dispatcher->connect('routing.load_configuration', array('doAuthRouting', 'listenToRoutingLoadConfigurationEvent'));
  74. }
  75. }
  76. * or create your own routes. Use a sample file located in plugins/doAuth/config/routing.samlpe.yml
  77. * write your own email templates. Copy all _mail_* partials from plugins/doAuth/modules/baseAuth/templates to your user/templates and rewrite them.
  78. * don't forget to set symfony default actions, like we did for baseAuth module
  79. login_module: user
  80. login_action: signin
  81. secure_module: user
  82. secure_action: secure
  83. Registration
  84. ------------
  85. You can extend registration form in your own way. Here are 2 typical cases.
  86. * To add custom widgets or validators to RegisterForm. Create new RegisterUserForm class in your lib/forms folder.
  87. [php]
  88. class RegisterUserForm extends BaseRegisterUserForm {
  89. public function configure()
  90. {
  91. parent::configure();
  92. // extend your code here
  93. }
  94. }
  95. Sometimes you need more complex schema. For example, register user with different profile types, for example: Client and Developer. In this case you need to embed a Client and Developer forms into RegistrationForm depending on request parameters. This can't be made just by extending registration class. In this case you can use an events to extend current Register action with your logic.
  96. Use ['user.pre_register'] event to access registration action, get request parameters, extend form, do everything you need.
  97. * Add this line to your frontendConfiguration class
  98. [php]
  99. $this->dispatcher->connect('user.pre_register', array('UserListener', 'registerWithRoles'));
  100. * create your listener class (that will act as a controller) and make it handle this event
  101. [php]
  102. class UserListener {
  103. public static function registerWithRoles(sfEvent $event) {
  104. // here comes a userActions controller
  105. $controller = $event->getSubject();
  106. // waiting for 'developer' or 'client' value
  107. $role = $controller->getRequest()->getParameter('role');
  108. $user = $controller->form->getObject();
  109. // all what we need for this example:
  110. $formclass = $role.'Form';
  111. $embed_form = new $formclass($user->get(ucfirst($role)));
  112. $controller->form->embedForm('role',$embed_form);
  113. }
  114. }
  115. Codes and Security
  116. ----------------------
  117. doAuthPlugin generates hashes for remember filter, activation code, password reset code and a new password for user on request.
  118. Here are the principles that doAuth follows to create user codes:
  119. * doAuth uses CSRF secret key to generate unique codes for every symfony site.
  120. * doAuth uses Salt, Email, Password fields from User record to generate codes.
  121. * doAuth uses sha1 hash algorithm to create code from strings.
  122. * doAuth generates random 10-symbol length password from all latin characters upper and lowercase and numbers on user request.
  123. If you are unhappy with provided methods, you can override them, following this instructions:
  124. * Copy doAuthTools.class.php located in plugins/doAuthPlugin/lib to your project lib folder.
  125. * Rewrite all functions there to your own
  126. * Clear symfony cache (yes, `symfony cc` thing)
  127. * Now doAuth fully depends on your own implementation of this class.
  128. (Great thanks to Laurent Bachelier and Andrei Dziahel)
  129. Configuration
  130. -------------
  131. This options are stored in plugins/doAuth/config/app.sample.yml.
  132. If you want to change some settings - copy them to your app.yml file.
  133. all:
  134. doAuth:
  135. # password encrypting algorithm
  136. algorithm_callable: sha1
  137. # function for delegating password check
  138. check_password_callable: false
  139. # coookie
  140. remember_cookie_name: doRemember
  141. # expiration time (in secs), currently 1 year
  142. remember_cookie_expiration_age: 31536000
  143. # use user activation
  144. activation: false
  145. # where to redirect after request for password reset
  146. reset_password_url: '@homepage'
  147. # signin redirect
  148. signin_url: '@homepage'
  149. #signout url
  150. signout_url: '@homepage'
  151. # register standard routes
  152. routes_register: true
  153. doAuth_register:
  154. # forward registration to next module
  155. # syntax: [module, action]
  156. forward: ~
  157. # or redirect to current path
  158. redirect_path: '@homepage'
  159. # auto sign in after registration
  160. signin: true
  161. doAuth_email:
  162. # activate by email if activation is on
  163. activation: true
  164. # send registration notification
  165. registration: true
  166. # sender email
  167. from: mailer@currenthost.com
  168. # module where email partials are stored.
  169. # default is module from controller
  170. module: false
  171. Events
  172. ------
  173. Here is a list of all events that are fired by doAuthPlugin:
  174. * user.signed_in - on sign in. Subject is doAuthSecurityUser class.
  175. * user.pre_register - runs before the registration starts. Can be
  176. overridden by inheritance. Subject - controller. Refer to Registration sections on usage of this event.
  177. * user.registered - on successfully completed registration. Subject is controller.
  178. * user.activated - on user successfully activation. Needs activation to be turned on.
  179. Basically 2 last events are used to send emails.
  180. Alternative to events (new in 0.9.5)
  181. ------------------------------------
  182. To extend user actions you don't have to assign listeners to events. You can also override this dummy methods in your controller.
  183. * preSignin - executed after the Signin form is created but before it was binded. Access for with $this->form
  184. * postSignin - executed when the user signs in. You can use this method for redirection.
  185. * preRegister - executed when Registration for is created but not binded. Use $this->form to access the form.
  186. * postRegister - executed after the new user is created, but not activated.
  187. * preActivate - executed before the Activate action runs
  188. * postActivate - executed when user is activated but not signed in
  189. Tasks (new in 0.9.5)
  190. -------
  191. Added several tasks for user management
  192. * user:create username password - creates user
  193. * user:promote username - promotes a user to superadmin
  194. * user:ban - deactivates (bans) user
  195. I18n
  196. ----
  197. All the messages and templates are I18n-ready. Please check doAuthMailer class to add translations to email subjects and doAuthActions to translate flash messages
  198. Contribute
  199. ----------
  200. You can always fork this project on Github.
  201. http://github.com/DavertMik/doAuthPlugin
  202. Bugfixes, enhancements, bugreports are always welcome.
  203. TODO
  204. ----
  205. * test everything, cover with functional tests