View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.campagnebp.service;
35  
36  import javax.servlet.http.HttpServletResponse;
37  
38  import fr.paris.lutece.plugins.campagnebp.business.MyAccount;
39  import fr.paris.lutece.plugins.campagnebp.business.MyInfosForm;
40  import fr.paris.lutece.plugins.mylutece.modules.openam.service.OpenamService;
41  import fr.paris.lutece.plugins.openamidentityclient.business.CreateAccountResult;
42  import fr.paris.lutece.plugins.openamidentityclient.business.Identity;
43  import fr.paris.lutece.plugins.openamidentityclient.business.SearchListAccountResult;
44  import fr.paris.lutece.plugins.openamidentityclient.service.OpenamIdentityException;
45  import fr.paris.lutece.plugins.openamidentityclient.service.OpenamIdentityService;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  import fr.paris.lutece.portal.web.LocalVariables;
48  
49  /**
50   *
51   */
52  public class AccountService
53  {
54  
55      /**
56       * return null if no error appear otherwise the error
57       * 
58       * @param formInfos
59       *            the formInfos
60       * @param formMyAccount
61       *            the form Account
62       * @return return null if no error appear otherwise the error
63       * @throws OpenamIdentityException
64       *             If exception with openam identity
65       */
66      public static CreateAccountResult createAccountAndIdentity( MyInfosForm formInfos, MyAccount formMyAccount ) throws OpenamIdentityException
67      {
68  
69          // create account
70          CreateAccountResult accountResult;
71          // create account
72          accountResult = OpenamIdentityService.getService( ).createAccount( formMyAccount.getLogin( ), formMyAccount.getPassword( ) );
73          try
74          {
75              // create identity
76              if ( accountResult != null )
77              {
78                  OpenamIdentityService.getService( ).createIdentity( convertMyInfosFormToIdentity( formInfos, accountResult.getUid( ) ) );
79              }
80  
81          }
82          catch( OpenamIdentityException e )
83          {
84  
85              // rall back create account
86              try
87              {
88                  OpenamIdentityService.getService( ).deleteAccount( accountResult.getUid( ) );
89              }
90              catch( OpenamIdentityException e1 )
91              {
92                  AppLogService.error( e1 );
93              }
94              throw e;
95  
96          }
97          // auto login the user
98          HttpServletResponse response = LocalVariables.getResponse( );
99          OpenamService.getInstance( ).setConnectionCookie( accountResult.getTokenId( ), (HttpServletResponse) response );
100         return accountResult;
101     }
102 
103     /**
104      * @param strEmail
105      *            The email
106      * @return Account UID
107      */
108     public static String getAccountUidByEmail( String strEmail )
109     {
110 
111         SearchListAccountResult searchListAccountResult = null;
112         try
113         {
114             searchListAccountResult = OpenamIdentityService.getService( ).getAccountList( strEmail, null, null, null, null );
115         }
116         catch( OpenamIdentityException e )
117         {
118             AppLogService.error( e );
119         }
120 
121         if ( searchListAccountResult != null && searchListAccountResult.getUsers( ).size( ) > 0 )
122         {
123             return searchListAccountResult.getUsers( ).get( 0 ).getUid( );
124         }
125 
126         return null;
127 
128     }
129 
130     /**
131      * 
132      * @param form
133      * @return
134      */
135     private static Identity convertMyInfosFormToIdentity( MyInfosForm form, String strUid )
136     {
137         Identity identity = new Identity( );
138         identity.setUid( strUid );
139         identity.setFirstname( form.getFirstname( ) );
140         identity.setLastname( form.getLastname( ) );
141         identity.setBirthday( form.getBirthdate( ) );
142         // identity.setPostalCode(form.getArrondissement());
143         // identity.setStreet(form.getCodePostalResidence());
144 
145         return identity;
146 
147     }
148 
149 }