View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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  
35  package fr.paris.lutece.plugins.identitystore.modules.cnicertifier.service;
36  
37  import fr.paris.lutece.plugins.identitystore.modules.cnicertifier.business.CNI;
38  import fr.paris.lutece.plugins.identitystore.web.rs.dto.AttributeDto;
39  import fr.paris.lutece.plugins.identitystore.web.rs.dto.AuthorDto;
40  import fr.paris.lutece.plugins.identitystore.web.rs.dto.CertificateDto;
41  import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityChangeDto;
42  import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityDto;
43  import fr.paris.lutece.plugins.identitystore.web.service.AuthorType;
44  import fr.paris.lutece.plugins.identitystore.web.service.IdentityService;
45  import fr.paris.lutece.portal.service.security.LuteceUser;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  
49  import java.util.HashMap;
50  import java.util.Map;
51  import java.util.concurrent.ConcurrentHashMap;
52  
53  /**
54   * CNICertifierService
55   */
56  public class CNICertifierService
57  {
58      private static final String BEAN_IDENTITYSTORE_SERVICE = "identitystore-cnicertifier.identitystore.service";
59      private static final String PROPERTY_IDENTITY_SERVICE_CLIENT_CODE = "identitystore-cnicertifier.identitystore.client.code";
60      private static final String CERTIFIER_CODE = "cnicertifier";
61      private static final String CLIENT_CODE = AppPropertiesService.getProperty( PROPERTY_IDENTITY_SERVICE_CLIENT_CODE );
62  
63      /**
64       * Certify attributes
65       * 
66       * @param user
67       *            The Lutece User
68       * @param cni
69       *            The CNI
70       */
71      public void certify( LuteceUser user, CNI cni )
72      {
73          IdentityService identityService = SpringContextService.getBean( BEAN_IDENTITYSTORE_SERVICE );
74  
75          IdentityChangeDto identityChange = new IdentityChangeDto( );
76          IdentityDto identity = new IdentityDto( );
77  
78          identity.setConnectionId( user.getName( ) );
79          Map<String, AttributeDto> mapAttributes = new ConcurrentHashMap<>( );
80          addAttribute( mapAttributes, "gender", cni.getGender( ) );
81          addAttribute( mapAttributes, "first_name", cni.getFirstName( ) );
82          addAttribute( mapAttributes, "family_name", cni.getLastName( ) );
83          addAttribute( mapAttributes, "birthdate", cni.getBirthDate( ) );
84          addAttribute( mapAttributes, "birthplace", cni.getBirthPlace( ) );
85  
86          identity.setAttributes( mapAttributes );
87          identityChange.setIdentity( identity );
88  
89          AuthorDto author = new AuthorDto( );
90          author.setApplicationCode( CLIENT_CODE );
91          author.setType( AuthorType.TYPE_USER_OWNER.getTypeValue( ) );
92          author.setId( AuthorDto.USER_DEFAULT_ID );
93          identityChange.setAuthor( author );
94  
95          identityService.updateIdentity( identityChange, new HashMap<>( ) );
96      }
97  
98      /**
99       * Add attribute DTO to a map
100      * 
101      * @param map
102      *            The map
103      * @param strKey
104      *            The attribute key
105      * @param strValue
106      *            The attribute value
107      */
108     private void addAttribute( Map<String, AttributeDto> map, String strKey, String strValue )
109     {
110         AttributeDto attribute = new AttributeDto( );
111         attribute.setKey( strKey );
112         attribute.setValue( strValue );
113         CertificateDto certificateDto = new CertificateDto( );
114         certificateDto.setCertifierCode( CERTIFIER_CODE );
115         attribute.setCertificate( certificateDto );
116         map.put( attribute.getKey( ), attribute );
117     }
118 
119 }