title: How to Encode and Decode password in symfony2
body: <p>In my project there is "forgot password functionality". When user do forgot password then email should go to user mail Id with decoded password. after he logins in then login functionality should be worked.</p>
  
  <p>I have used the following encoders.</p>
  
  <pre><code>Secuirty.yml:
      security:
          encoders:
              Coupon\EntityBundle\Entity\SupportUsers:
                  algorithm: sha1
                  encode_as_base64: false
                  iterations: 1
  
      #    encoders:
      #        Coupon\EntityBundle\Entity\SupportUsers: plaintext
  
          role_hierarchy:
              ROLE_ADMIN:       ROLE_USER
              ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  
          providers:
              main:
                  entity:
                      class: Coupon\EntityBundle\Entity\SupportUsers
                      property: supportUserUsername
  
          firewalls:
              dev:
                  pattern:  ^/(_(profiler|wdt)|css|images|js)/
                  security: false
  
              login:
                  pattern:  ^/login$
                  security: false
  
              coupon_login_forgot_password:
                  pattern:  ^/forgot-password$
                  security: false
  
      #        coupon_report_categoryWiseCSV:
      #            pattern:  ^/report/csv-category$
      #            security: false
  
      #        default_security_target:
      #            pattern:  ^/users/home$
      #            security: false
  
              coupon_dashboard:
                  pattern:    ^/
                  form_login:
                      check_path: login_check
                      login_path: login
                      default_target_path: coupon_dashboard
                      always_use_default_target_path: true
                  logout:
                      path:   logout
                      target: login
                  remember_me:
                      key: trex-kharadi6
                      lifetime: 43200
                      always_remember_me: false
                      remember_me_parameter: _remember_me
                      path: ~
                      domain: ~
                  #anonymous: ~
                  #http_basic:
                  #    realm: "Secured Demo Area"
  
          access_control:
              - { path: ^/coupon_dashboard, roles: ROLE_ADMIN }
              - { path: ^/login, roles: IS_AUTHENTICATED_FULLY}
  </code></pre>
  
  <p>How to write custom encode and decode methods and how to configure it.</p>
  
  <p>Forgot password Action:
      Here, I am retrieving the encoded password from database. </p>
  
  <pre><code>public function forgotPasswordAction(Request $request) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $username = $request-&gt;get('username');
              $couponHelper = $this-&gt;get('coupon_helper');
  
              if ($request-&gt;isMethod('POST')) {
  
  
                  //echo $username; die;
                  if ($username == '') {
                      $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('msg', $this-&gt;get('translator')-&gt;trans('forgot.pwd.enter'));
                  } else {
                      $user = $em-&gt;getRepository('CouponEntityBundle:SupportUsers')-&gt;findBy(array('supportUserUsername' =&gt; trim($username), 'supportUserStatus' =&gt; '0'));
                      if (count($user) != 0) {
                          $password = $user[0]-&gt;getSupportUserPassword();
                          $email = $user[0]-&gt;getSupportUserEmail();
                          if ($email != '') {
  
                              //emailer helper.
                              $data['firstName']='';
                              $data['lastName']='';
                              $data['userName'] = $user[0]-&gt;getSupportUserUsername()?$user[0]-&gt;getSupportUserUsername():'';                      
                              $data['email'] = $email;
                              $data['password'] = $password ? $password:'';
                              $data['instance'] = 'forgotpassword';
                              $emailStatus = $couponHelper-&gt;emailer($data);
                              $logger = $this-&gt;get('logger');
                              $logger-&gt;info('[Email]: [' . $emailStatus . ']');
                              unset($data);
  
                              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('msg', $this-&gt;get('translator')-&gt;trans('forgot.pwd.sent'));
                          } else {
                              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('error', $this-&gt;get('translator')-&gt;trans('forgot.pwd.reg.not.email'));
                          }
                      } else {
                          $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('error', $this-&gt;get('translator')-&gt;trans('forgot.pwd.not.reg'));
                      }
                  }
  
                  return $this-&gt;redirect($this-&gt;generateUrl('coupon_login_forgot_password'));
              }
  
              return $this-&gt;render('CouponLoginBundle:Login:forgot_password.html.twig', array('username' =&gt; $username));
          }
  </code></pre>
  
  <p>So, in forgot password action how to get original password and mail it the user. Kindly help me out.</p>