com.formkiq.server.api.SystemController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
/*
* Copyright (C) 2016 FormKiQ Inc.
*
* 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 com.formkiq.server.api;
import static com.formkiq.server.service.SystemPropertyService.KEY_HOSTNAME;
import static org.springframework.util.StringUtils.isEmpty;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.formkiq.server.domain.type.SystemPropertyListDTO;
import com.formkiq.server.domain.type.UserRole;
import com.formkiq.server.domain.type.UserStatus;
import com.formkiq.server.service.AuthenticationFailureException;
import com.formkiq.server.service.OAuthService;
import com.formkiq.server.service.PreconditionFailedException;
import com.formkiq.server.service.SystemPropertyService;
import com.formkiq.server.service.UserService;
/**
* System Services.
*
*/
@RestController
public class SystemController extends AbstractRestController {
/** System Setup URL. */
public static final String API_SYSTEM_SETUP = "/api/setup";
/** System Ping URL. */
public static final String API_SYSTEM_PING = "/api/ping";
/** System Version URL. */
public static final String API_SYSTEM_VERSION = "/api/version";
/** System Properties URL. */
public static final String API_SYSTEM_PROPERTIES_GET
= "/api/properties/get";
/** Save System Properties URL. */
public static final String API_SYSTEM_PROPERTIES_SAVE
= "/api/properties/save";
/** Delete System Properties URL. */
public static final String API_SYSTEM_PROPERTIES_DELETE
= "/api/properties/delete";
/** OAuthService. */
@Autowired
private OAuthService oauthservice;
/** SystemPropertyService. */
@Autowired
private SystemPropertyService systemProperties;
/** UserService. */
@Autowired
private UserService userservice;
/**
* Delete System Property.
* @param key {@link String}
* @return {@link ApiMessageResponse}
*/
@Secured({ "ROLE_ADMIN" })
@Transactional
@RequestMapping(API_SYSTEM_PROPERTIES_DELETE)
public ApiMessageResponse deleteProperties(
@RequestParam(value = "key", required = true)
final String key) {
this.systemProperties.delete(key);
return new ApiMessageResponse("Property Deleted");
}
/**
* System Ping Response.
* @return {@link String}
*/
@RequestMapping(API_SYSTEM_PING)
public String ping() {
return "ok";
}
/**
* System Version Response.
* @return {@link ApiMessageResponse}
*/
@RequestMapping(API_SYSTEM_VERSION)
public ApiMessageResponse version() {
String version = this.systemProperties.getVersion();
return new ApiMessageResponse(version);
}
/**
* Get System Properties.
* @return {@link String}
*/
@Secured({ "ROLE_ADMIN" })
@Transactional
@RequestMapping(API_SYSTEM_PROPERTIES_GET)
public SystemPropertyListDTO properties() {
return this.systemProperties.getProperties();
}
/**
* Save System Property.
* @param key {@link String}
* @param value {@link String}
* @return {@link ApiMessageResponse}
*/
@Secured({ "ROLE_ADMIN" })
@Transactional
@RequestMapping(API_SYSTEM_PROPERTIES_SAVE)
public ApiMessageResponse saveProperties(
@RequestParam(value = "key", required = true)
final String key,
@RequestParam(value = "value", required = true)
final String value) {
this.systemProperties.save(key, value);
return new ApiMessageResponse("Property Saved");
}
/**
* Setup System, this can be only called once per system.
* @param clientname {@link String}
* @param client {@link String}
* @param clientsecret {@link String}
* @param email {@link String}
* @param hostname {@link String}
* @param password {@link String}
* @param confirmPassword {@link String}
* @return {@link ApiMessageResponse}
*/
@Transactional
@RequestMapping(API_SYSTEM_SETUP)
public ApiMessageResponse setup(
@RequestParam(value = "clientname", required = true)
final String clientname,
@RequestParam(value = "client", required = false)
final String client,
@RequestParam(value = "clientsecret", required = false)
final String clientsecret,
@RequestParam(value = "email", required = true)
final String email,
@RequestParam(value = KEY_HOSTNAME, required = true)
final String hostname,
@RequestParam(value = "password", required = true)
final String password,
@RequestParam(value = "confirmpassword", required = true)
final String confirmPassword) {
if (this.oauthservice.clientCount() > 0) {
throw new AuthenticationFailureException(
"System already configured");
}
if (isEmpty(clientname) || isEmpty(email) || isEmpty(password)
|| isEmpty(confirmPassword)) {
throw new PreconditionFailedException("Not all fields entered");
}
if (!password.equals(confirmPassword)) {
throw new PreconditionFailedException("Passwords do not match");
}
String clientstr = StringUtils.isEmpty(client)
? this.userservice.generateSecurityToken() : client;
String clientSecretStr = StringUtils.isEmpty(clientsecret)
? this.userservice.generateSecurityToken() : clientsecret;
this.oauthservice.addClientDetails(clientname, clientstr,
clientSecretStr);
this.systemProperties.setInviteOnly(false);
this.userservice.createUser(email, password,
UserStatus.ACTIVE, UserRole.ROLE_ADMIN);
this.systemProperties.setSystemHostname(hostname);
this.systemProperties.setInviteOnly(true);
return new ApiMessageResponse(
"setup complete ... Login - Client: "
+ clientstr + " - Client Secret: " + clientSecretStr);
}
}