001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.scim.server.mapper; 022 023import java.net.URI; 024 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.NuxeoException; 027import org.nuxeo.ecm.platform.usermanager.UserManager; 028import org.nuxeo.runtime.api.Framework; 029 030import com.unboundid.scim.data.GroupResource; 031import com.unboundid.scim.data.Meta; 032import com.unboundid.scim.data.UserResource; 033import com.unboundid.scim.schema.CoreSchema; 034 035/** 036 * Base class used for mappers 037 * 038 * @author tiry 039 * @since 7.4 040 */ 041public abstract class AbstractMapper { 042 043 protected UserManager um; 044 045 protected final String baseUrl; 046 047 public AbstractMapper(String baseUrl) { 048 um = Framework.getService(UserManager.class); 049 this.baseUrl = baseUrl; 050 } 051 052 public GroupResource getGroupResourceFromNuxeoGroup(DocumentModel groupModel) throws Exception { 053 054 GroupResource groupResource = new GroupResource(CoreSchema.GROUP_DESCRIPTOR); 055 056 String groupId = (String) groupModel.getProperty(um.getGroupSchemaName(), um.getGroupIdField()); 057 058 URI location = new URI(baseUrl + "/" + groupId); 059 Meta meta = new Meta(null, null, location, "1"); 060 groupResource.setMeta(meta); 061 062 String groupLabel = (String) groupModel.getProperty(um.getGroupSchemaName(), um.getGroupLabelField()); 063 064 groupResource.setDisplayName(groupLabel); 065 groupResource.setId(groupId); 066 groupResource.setExternalId(groupId); 067 068 return groupResource; 069 } 070 071 public DocumentModel createGroupModelFromGroupResource(GroupResource group) throws NuxeoException { 072 073 if (um.getGroup(group.getId()) == null) { 074 DocumentModel newGroup = um.getBareGroupModel(); 075 076 String groupId = group.getId(); 077 if (groupId == null || groupId.isEmpty()) { 078 groupId = group.getDisplayName(); 079 } 080 newGroup.setProperty(um.getGroupSchemaName(), um.getGroupIdField(), groupId); 081 updateGroupModel(newGroup, group); 082 return um.createGroup(newGroup); 083 } else { 084 return updateGroupModelFromGroupResource(group.getId(), group); 085 } 086 } 087 088 public DocumentModel updateGroupModelFromGroupResource(String uid, GroupResource group) throws NuxeoException { 089 090 DocumentModel groupModel = um.getGroupModel(uid); 091 if (groupModel == null) { 092 return null; 093 } 094 updateGroupModel(groupModel, group); 095 um.updateGroup(groupModel); 096 return groupModel; 097 } 098 099 protected void updateGroupModel(DocumentModel userModel, GroupResource groupResouce) throws NuxeoException { 100 String displayName = groupResouce.getDisplayName(); 101 if (displayName != null && !displayName.isEmpty()) { 102 userModel.setProperty(um.getGroupSchemaName(), um.getGroupLabelField(), displayName); 103 } 104 } 105 106 public abstract UserResource getUserResourceFromNuxeoUser(DocumentModel userModel) throws Exception; 107 108 public abstract DocumentModel createNuxeoUserFromUserResource(UserResource user) throws NuxeoException; 109 110 public abstract DocumentModel updateNuxeoUserFromUserResource(String uid, UserResource user); 111 112}