/* * 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. */ package org.maxkey.webapi.identity.scim; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.mybatis.jpa.persistence.JpaPageResults; import org.maxkey.constants.ConstsStatus; import org.maxkey.entity.Roles; import org.maxkey.entity.UserInfo; import org.maxkey.persistence.service.RolesService; import org.maxkey.persistence.service.UserInfoService; import org.maxkey.util.DateUtils; import org.maxkey.util.StringUtils; import org.maxkey.webapi.identity.scim.resources.ScimEnterprise; import org.maxkey.webapi.identity.scim.resources.ScimFormattedName; import org.maxkey.webapi.identity.scim.resources.ScimGroupRef; import org.maxkey.webapi.identity.scim.resources.ScimManager; import org.maxkey.webapi.identity.scim.resources.ScimMeta; import org.maxkey.webapi.identity.scim.resources.ScimParameters; import org.maxkey.webapi.identity.scim.resources.ScimSearchResult; import org.maxkey.webapi.identity.scim.resources.ScimUser; import org.maxkey.webapi.identity.scim.resources.ScimUserEmail; import org.maxkey.webapi.identity.scim.resources.ScimUserPhoneNumber; import org.maxkey.webapi.identity.scim.resources.ScimOrganizationEmail.UserEmailType; import org.maxkey.webapi.identity.scim.resources.ScimOrganizationPhoneNumber.UserPhoneNumberType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.converter.json.MappingJacksonValue; import org.springframework.web.bind.annotation.ModelAttribute; 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 User *
* http://tools.ietf.org/html/draft-ietf-scim-core-schema-00#section-6 *
* it is based on the SCIM 2.0 API Specification: *
* http://tools.ietf.org/html/draft-ietf-scim-api-00#section-3
*/
@RestController
@RequestMapping(value = "/api/idm/SCIM/v2/Users")
public class ScimUserController {
final static Logger _logger = LoggerFactory.getLogger(ScimUserController.class);
@Autowired
private UserInfoService userInfoService;
@Autowired
RolesService rolesService;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public MappingJacksonValue get(@PathVariable String id,
@RequestParam(required = false) String attributes) {
UserInfo userInfo = userInfoService.get(id);
ScimUser scimUser = userInfo2ScimUser(userInfo);
return new MappingJacksonValue(scimUser);
}
@RequestMapping(method = RequestMethod.POST)
public MappingJacksonValue create(@RequestBody ScimUser user,
@RequestParam(required = false) String attributes,
UriComponentsBuilder builder) throws IOException {
UserInfo userInfo = scimUser2UserInfo(user);
userInfoService.insert(userInfo);
return get(userInfo.getId(),attributes);
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public MappingJacksonValue replace(@PathVariable String id,
@RequestBody ScimUser user,
@RequestParam(required = false) String attributes)
throws IOException {
UserInfo userInfo = scimUser2UserInfo(user);
userInfoService.update(userInfo);
return get(id,attributes);
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void delete(@PathVariable final String id) {
userInfoService.remove(id);
}
@RequestMapping(method = RequestMethod.GET)
public MappingJacksonValue searchWithGet(@ModelAttribute ScimParameters requestParameters) {
return searchWithPost(requestParameters);
}
@RequestMapping(value = "/.search", method = RequestMethod.POST)
public MappingJacksonValue searchWithPost(@ModelAttribute ScimParameters requestParameters) {
requestParameters.parse();
_logger.debug("requestParameters {} ",requestParameters);
UserInfo queryModel = new UserInfo();
queryModel.setPageSize(requestParameters.getCount());
queryModel.calculate(requestParameters.getStartIndex());
JpaPageResults