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.console.panels;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.stream.Collectors;
26  import org.apache.syncope.client.console.SyncopeConsoleSession;
27  import org.apache.syncope.client.console.rest.ConnectorRestClient;
28  import org.apache.syncope.client.console.rest.ResourceRestClient;
29  import org.apache.syncope.client.ui.commons.Constants;
30  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
31  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
32  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
33  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
34  import org.apache.syncope.common.lib.SyncopeConstants;
35  import org.apache.syncope.common.lib.to.ResourceTO;
36  import org.apache.syncope.common.lib.to.VirSchemaTO;
37  import org.apache.syncope.common.lib.types.IdMEntitlement;
38  import org.apache.wicket.ajax.AjaxRequestTarget;
39  import org.apache.wicket.model.PropertyModel;
40  import org.apache.wicket.spring.injection.annot.SpringBean;
41  
42  public class VirSchemaDetails extends AbstractSchemaDetailsPanel {
43  
44      private static final long serialVersionUID = 5979623248182851337L;
45  
46      @SpringBean
47      protected ResourceRestClient resourceRestClient;
48  
49      @SpringBean
50      protected ConnectorRestClient connectorRestClient;
51  
52      protected final Map<String, String> anyTypes = new HashMap<>();
53  
54      protected final AjaxDropDownChoicePanel<String> anyType;
55  
56      protected ResourceTO selectedResource;
57  
58      public VirSchemaDetails(final String id, final VirSchemaTO schemaTO) {
59          super(id, schemaTO);
60  
61          AjaxCheckBoxPanel readonly = new AjaxCheckBoxPanel("readonly", getString("readonly"),
62                  new PropertyModel<>(schemaTO, "readonly"));
63          add(readonly);
64  
65          AjaxDropDownChoicePanel<String> resource = new AjaxDropDownChoicePanel<>(
66                  "resource", getString("resource"), new PropertyModel<String>(schemaTO, "resource"), false).
67                  setNullValid(false);
68          resource.setChoices(resourceRestClient.list().stream().map(ResourceTO::getKey).collect(Collectors.toList()));
69          resource.setOutputMarkupId(true);
70          resource.addRequiredLabel();
71          if (resource.getModelObject() != null) {
72              populateAnyTypes(resource.getModelObject());
73          }
74          add(resource);
75  
76          anyType = new AjaxDropDownChoicePanel<>(
77                  "anyType", getString("anyType"), new PropertyModel<String>(schemaTO, "anyType"), false).
78                  setNullValid(false);
79          anyType.setChoices(new ArrayList<>(anyTypes.keySet()));
80          anyType.setOutputMarkupId(true);
81          anyType.setOutputMarkupPlaceholderTag(true);
82          anyType.addRequiredLabel();
83          if (resource.getModelObject() == null) {
84              anyType.setEnabled(false);
85          }
86          add(anyType);
87  
88          AjaxTextFieldPanel extAttrName = new AjaxTextFieldPanel(
89                  "extAttrName", getString("extAttrName"), new PropertyModel<>(schemaTO, "extAttrName"));
90          extAttrName.setOutputMarkupId(true);
91          extAttrName.addRequiredLabel();
92          if (selectedResource != null) {
93              extAttrName.setChoices(getExtAttrNames());
94          }
95          add(extAttrName);
96  
97          resource.getField().add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
98  
99              private static final long serialVersionUID = -1107858522700306810L;
100 
101             @Override
102             protected void onUpdate(final AjaxRequestTarget target) {
103                 anyTypes.clear();
104                 if (resource.getModelObject() != null) {
105                     populateAnyTypes(resource.getModelObject());
106                     anyType.setEnabled(true);
107                 }
108                 anyType.setChoices(new ArrayList<>(anyTypes.keySet()));
109                 anyType.setModelObject(null);
110                 target.add(anyType);
111             }
112         });
113 
114         anyType.getField().add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
115 
116             private static final long serialVersionUID = -1107858522700306810L;
117 
118             @Override
119             protected void onUpdate(final AjaxRequestTarget target) {
120                 if (selectedResource != null) {
121                     String adminRealm = getAdminRealm(selectedResource.getConnector());
122 
123                     if (SyncopeConsoleSession.get().owns(IdMEntitlement.CONNECTOR_READ, adminRealm)) {
124                         extAttrName.setChoices(getExtAttrNames());
125                         target.add(extAttrName);
126                     }
127                 }
128             }
129         });
130     }
131 
132     protected String getAdminRealm(final String connectorKey) {
133         String adminRealm = null;
134         try {
135             adminRealm = connectorRestClient.read(connectorKey).getAdminRealm();
136         } catch (Exception e) {
137             LOG.error("Could not read Admin Realm for External Resource {}", selectedResource.getKey());
138         }
139 
140         return adminRealm;
141     }
142 
143     protected void populateAnyTypes(final String resourceKey) {
144         anyTypes.clear();
145         if (resourceKey != null) {
146             ResourceTO resource = resourceRestClient.read(resourceKey);
147             String adminRealm = getAdminRealm(resource.getConnector());
148 
149             if (SyncopeConsoleSession.get().owns(IdMEntitlement.RESOURCE_READ, adminRealm)) {
150                 selectedResource = resource;
151                 selectedResource.getProvisions().forEach(
152                         provisionTO -> anyTypes.put(provisionTO.getAnyType(), provisionTO.getObjectClass()));
153             }
154         }
155     }
156 
157     protected List<String> getExtAttrNames() {
158         return connectorRestClient.getExtAttrNames(
159                 SyncopeConstants.ROOT_REALM,
160                 anyTypes.get(anyType.getModelObject()),
161                 selectedResource.getConnector(),
162                 selectedResource.getConfOverride());
163     }
164 }