View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.enduser.pages;
20  
21  import com.fasterxml.jackson.core.JsonProcessingException;
22  import com.fasterxml.jackson.databind.json.JsonMapper;
23  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
24  import org.apache.syncope.client.enduser.SyncopeWebApplication;
25  import org.apache.syncope.client.enduser.layout.UserFormLayoutInfo;
26  import org.apache.syncope.client.enduser.panels.UserSelfFormPanel;
27  import org.apache.syncope.common.lib.SyncopeConstants;
28  import org.apache.syncope.common.lib.to.UserTO;
29  import org.apache.wicket.markup.html.WebMarkupContainer;
30  import org.apache.wicket.request.mapper.parameter.PageParameters;
31  
32  public class SelfRegistration extends BasePage {
33  
34      private static final long serialVersionUID = -1100228004207271270L;
35  
36      private static final String SELF_REGISTRATION = "page.selfRegistration";
37  
38      public static final String NEW_USER_PARAM = "newUser";
39  
40      protected static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
41  
42      public SelfRegistration(final PageParameters parameters) {
43          super(parameters, SELF_REGISTRATION);
44  
45          setDomain(parameters);
46          disableSidebar();
47  
48          WebMarkupContainer content = new WebMarkupContainer("content");
49          content.setOutputMarkupId(true);
50          contentWrapper.add(content);
51  
52          UserSelfFormPanel selfRegistrationPanel = new UserSelfFormPanel(
53                  "selfRegistrationPanel",
54                  buildNewUserTO(parameters),
55                  buildNewUserTO(parameters),
56                  SyncopeEnduserSession.get().getAnonymousClient().platform().getUserClasses(),
57                  buildFormLayout(),
58                  getPageReference());
59          selfRegistrationPanel.setOutputMarkupId(true);
60          content.add(selfRegistrationPanel);
61      }
62  
63      private UserFormLayoutInfo buildFormLayout() {
64          UserFormLayoutInfo customlayoutInfo = SyncopeWebApplication.get().getCustomFormLayout();
65          return customlayoutInfo != null ? customlayoutInfo : new UserFormLayoutInfo();
66      }
67  
68      private static UserTO buildNewUserTO(final PageParameters parameters) {
69          UserTO userTO = null;
70          if (parameters != null) {
71              if (!parameters.get(NEW_USER_PARAM).isNull()) {
72                  try {
73                      userTO = MAPPER.readValue(parameters.get(NEW_USER_PARAM).toString(), UserTO.class);
74                  } catch (JsonProcessingException e) {
75                      LOG.error("While reading user data from social registration", e);
76                  }
77              }
78          }
79          if (userTO == null) {
80              userTO = new UserTO();
81          }
82          userTO.setRealm(SyncopeConstants.ROOT_REALM);
83          return userTO;
84      }
85  }