ScimOrganizationController.java 7.7 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
18 19 20
package org.maxkey.identity.scim.controller;

import java.io.IOException;
M
MaxKey 已提交
21 22
import java.util.ArrayList;
import java.util.List;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
23

M
MaxKey 已提交
24 25 26 27 28
import org.apache.mybatis.jpa.persistence.JpaPageResults;
import org.maxkey.entity.Organizations;
import org.maxkey.identity.scim.resources.ScimMeta;
import org.maxkey.identity.scim.resources.ScimOrganization;
import org.maxkey.identity.scim.resources.ScimParameters;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
29
import org.maxkey.identity.scim.resources.ScimSearchResult;
M
MaxKey 已提交
30 31 32 33 34 35
import org.maxkey.persistence.service.OrganizationsService;
import org.maxkey.util.DateUtils;
import org.maxkey.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
36 37
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.json.MappingJacksonValue;
M
MaxKey 已提交
38
import org.springframework.web.bind.annotation.ModelAttribute;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * This Controller is used to manage Organization
 * <p>
 * http://tools.ietf.org/html/draft-ietf-scim-core-schema-00#section-6
 * <p>
 * it is based on the SCIM 2.0 API Specification:
 * <p>
 * http://tools.ietf.org/html/draft-ietf-scim-api-00#section-3
 */
@RestController
M
MaxKey 已提交
58
@RequestMapping(value = "/api/idm/SCIM/v2/Organization")
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
59
public class ScimOrganizationController {
M
MaxKey 已提交
60 61 62 63 64
	final static Logger _logger = LoggerFactory.getLogger(ScimOrganizationController.class);
	
	@Autowired
	OrganizationsService organizationsService;
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
65
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
M
MaxKey 已提交
66
    public MappingJacksonValue get(@PathVariable String id,
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
67
                                       @RequestParam(required = false) String attributes) {
M
MaxKey 已提交
68 69 70 71
    	Organizations	org = organizationsService.get(id);
    	ScimOrganization 	scimOrg = org2ScimOrg(org);
        
        return new MappingJacksonValue(scimOrg);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
72 73 74
    }

    @RequestMapping(method = RequestMethod.POST)
M
MaxKey 已提交
75 76 77 78 79 80
    public MappingJacksonValue create(@RequestBody  ScimOrganization scimOrg,
                                      @RequestParam(required = false) String attributes,
                                      UriComponentsBuilder builder) throws IOException {
        Organizations createOrg = scimOrg2Org(scimOrg);
        organizationsService.insert(createOrg);
        return get(createOrg.getId(), attributes);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
81 82 83
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
M
MaxKey 已提交
84 85 86 87 88 89
    public MappingJacksonValue replace(@PathVariable String id,
                                       @RequestBody ScimOrganization scimOrg,
                                       @RequestParam(required = false) String attributes)throws IOException {
    	Organizations updateOrg = scimOrg2Org(scimOrg);
    	organizationsService.update(updateOrg);
        return get(id, attributes);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
90 91 92 93 94
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public void delete(@PathVariable final String id) {
M
MaxKey 已提交
95
    	organizationsService.remove(id);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
96 97 98
    }

    @RequestMapping(method = RequestMethod.GET)
M
MaxKey 已提交
99
    public MappingJacksonValue searchWithGet(@ModelAttribute ScimParameters requestParameters) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
100 101 102 103
        return searchWithPost(requestParameters);
    }

    @RequestMapping(value = "/.search", method = RequestMethod.POST)
M
MaxKey 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    public MappingJacksonValue searchWithPost(@ModelAttribute ScimParameters requestParameters) {
    	requestParameters.parse();
    	_logger.debug("requestParameters {} ",requestParameters);
        Organizations queryModel = new Organizations();
        queryModel.setPageSize(requestParameters.getCount());
        queryModel.calculate(requestParameters.getStartIndex()); 
        
        JpaPageResults<Organizations> orgResults = organizationsService.queryPageResults(queryModel);
        List<ScimOrganization> resultList = new ArrayList<ScimOrganization>();
        for(Organizations org : orgResults.getRows()) {
        	resultList.add(org2ScimOrg(org));
        }
        ScimSearchResult<ScimOrganization> scimSearchResult = 
        		new ScimSearchResult<ScimOrganization>(
        				resultList,
        				orgResults.getRecords(),
        				requestParameters.getCount(),
        				requestParameters.getStartIndex());  
        
        return new MappingJacksonValue(scimSearchResult);
    }
    
    public ScimOrganization org2ScimOrg(Organizations org) {
    	ScimOrganization 	scimOrg = new ScimOrganization();
        scimOrg.setId(org.getId());
        scimOrg.setCode(org.getCode());
        scimOrg.setName(org.getName());
        scimOrg.setDisplayName(org.getName());
        scimOrg.setFullName(org.getFullName());
        scimOrg.setType(org.getType());
        scimOrg.setLevel(org.getLevel());
        scimOrg.setDivision(org.getDivision());
        scimOrg.setSortOrder(org.getSortOrder());
        scimOrg.setCodePath(org.getCodePath());
        scimOrg.setNamePath(org.getNamePath());
        scimOrg.setDescription(org.getDescription());
MaxKey单点登录官方's avatar
v3.0.0  
MaxKey单点登录官方 已提交
140
        
M
MaxKey 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        scimOrg.setParentId(org.getParentId());
        scimOrg.setParent(org.getParentId());
        //scimOrg.setParentCode(org.getParentId());
        scimOrg.setParentName(org.getParentName());
        
        scimOrg.setParentName(org.getParentName());
        if(StringUtils.isNotBlank(org.getSortOrder())) {
        	scimOrg.setOrder(Long.parseLong(org.getSortOrder()));
        }else {
        	scimOrg.setOrder(1);
        }
        scimOrg.setExternalId(org.getId());
        
        ScimMeta meta = new ScimMeta("Organization");
        
        if(StringUtils.isNotBlank(org.getCreatedDate())){
        	meta.setCreated(
        			DateUtils.parse(org.getCreatedDate(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS));
        }
        if(StringUtils.isNotBlank(org.getModifiedDate())){
        	meta.setLastModified(
        			DateUtils.parse(org.getModifiedDate(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS));
        }
        scimOrg.setMeta(meta);
        return scimOrg;
    }
    
    public Organizations scimOrg2Org(ScimOrganization 	scimOrg) {
    	Organizations org = new Organizations();
    	org.setId(scimOrg.getId());
    	org.setCode(scimOrg.getCode());
    	org.setFullName(scimOrg.getFullName());
    	org.setName(StringUtils.isNotBlank(scimOrg.getName()) ? scimOrg.getName():scimOrg.getDisplayName());
    	org.setParentId(StringUtils.isNotBlank(scimOrg.getParentId())? scimOrg.getParentId():scimOrg.getParent());
    	org.setParentCode(scimOrg.getParentCode());
    	org.setParentName(scimOrg.getParentName());
    	org.setSortOrder(StringUtils.isNotBlank(scimOrg.getSortOrder() )?scimOrg.getSortOrder():scimOrg.getOrder()+"");
    	org.setLevel(scimOrg.getLevel());
    	org.setType(scimOrg.getType());
    	org.setDivision(scimOrg.getDivision());
    	org.setDescription(scimOrg.getDescription());
    	return org;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
183 184
    }
}