use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.
the class UserRestController method createNewUser.
/**
* Create a new User
* @param model
* @param request
* @return
* @throws RestValidationFailedException
*/
@ApiOperation(value = "Create New User", notes = "Cannot save existing user")
@ApiResponses({ @ApiResponse(code = 201, message = "User Created", response = UserModel.class), @ApiResponse(code = 401, message = "Unauthorized Access", response = ResponseEntity.class), @ApiResponse(code = 409, message = "User Already Exists") })
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" })
public ResponseEntity<UserModel> createNewUser(@ApiParam(value = "User to save", required = true) @RequestBody(required = true) UserModel model, UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
RestProcessResult<UserModel> result = new RestProcessResult<UserModel>(HttpStatus.CREATED);
User user = this.checkUser(request, result);
if (result.isOk()) {
User u = UserDao.instance.getUser(model.getUsername());
if (Permissions.hasAdmin(user)) {
if (u == null) {
// Create new user
model.getData().setId(Common.NEW_ID);
if (model.validate()) {
try {
User newUser = model.getData();
newUser.setPassword(Common.encrypt(model.getData().getPassword()));
UserDao.instance.saveUser(newUser);
URI location = builder.path("v1/users/{username}").buildAndExpand(model.getUsername()).toUri();
result.addRestMessage(getResourceCreatedMessage(location));
return result.createResponseEntity(model);
} catch (Exception e) {
result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
return result.createResponseEntity();
}
} else {
result.addRestMessage(this.getValidationFailedError());
return result.createResponseEntity(model);
}
} else {
model.addValidationMessage(new ProcessMessage("username", new TranslatableMessage("users.validate.usernameInUse")));
result.addRestMessage(getValidationFailedError());
return result.createResponseEntity(model);
}
} else {
LOG.warn("Non admin user: " + user.getUsername() + " attempted to create user : " + model.getUsername());
result.addRestMessage(this.getUnauthorizedMessage());
return result.createResponseEntity();
}
}
return result.createResponseEntity();
}
use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.
the class UserRestController method updateHomeUrl.
@ApiOperation(value = "Update a user's home url")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json", "text/csv" }, value = "/{username}/homepage")
public ResponseEntity<UserModel> updateHomeUrl(@ApiParam(value = "Username", required = true, allowMultiple = false) @PathVariable String username, @ApiParam(value = "Home Url", required = true, allowMultiple = false) @RequestParam(required = true) String url, HttpServletRequest request, Authentication authentication) throws RestValidationFailedException {
RestProcessResult<UserModel> result = new RestProcessResult<UserModel>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
User u = UserDao.instance.getUser(username);
if (Permissions.hasAdmin(user)) {
if (u == null) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
}
if (u.getId() == user.getId() && !(authentication instanceof UsernamePasswordAuthenticationToken)) {
throw new AccessDeniedException(new TranslatableMessage("rest.error.usernamePasswordOnly"));
}
u.setHomeUrl(url);
UserModel model = new UserModel(u);
if (!model.validate()) {
result.addRestMessage(this.getValidationFailedError());
} else {
UserDao.instance.saveHomeUrl(u.getId(), url);
sessionRegistry.userUpdated(request, u);
}
return result.createResponseEntity(model);
} else {
if (u.getId() != user.getId()) {
LOG.warn("Non admin user: " + user.getUsername() + " attempted to access user : " + u.getUsername());
result.addRestMessage(this.getUnauthorizedMessage());
return result.createResponseEntity();
} else {
u.setHomeUrl(url);
UserModel model = new UserModel(u);
// Allow users to update themselves
model.getData().setId(u.getId());
if (!model.validate()) {
result.addRestMessage(this.getValidationFailedError());
} else {
// We have confirmed that we are the user
UserDao.instance.saveHomeUrl(u.getId(), url);
sessionRegistry.userUpdated(request, u);
}
return result.createResponseEntity(model);
}
}
}
return result.createResponseEntity();
}
use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.
the class UserRestController method deleteUser.
@ApiOperation(value = "Delete A User")
@RequestMapping(method = RequestMethod.DELETE, produces = { "application/json", "text/csv" }, value = "/{username}")
public ResponseEntity<UserModel> deleteUser(@ApiParam(value = "Username", required = true, allowMultiple = false) @PathVariable String username, HttpServletRequest request) throws RestValidationFailedException {
RestProcessResult<UserModel> result = new RestProcessResult<UserModel>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
User u = UserDao.instance.getUser(username);
if (u == null) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
}
UserModel model = new UserModel(u);
if (Permissions.hasAdmin(user)) {
if (u.getId() == user.getId()) {
model.addValidationMessage(new ProcessMessage("username", new TranslatableMessage("users.validate.badDelete")));
result.addRestMessage(getValidationFailedError());
return result.createResponseEntity(model);
}
UserDao.instance.deleteUser(u.getId());
return result.createResponseEntity(model);
} else {
LOG.warn("Non admin user: " + user.getUsername() + " attempted to delete user : " + u.getUsername());
result.addRestMessage(this.getUnauthorizedMessage());
}
}
return result.createResponseEntity();
}
use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.
the class AuditRestController method queryRQL.
@ApiOperation(value = "Query Audit Events", notes = "Admin access only", response = AuditEventInstanceModel.class, responseContainer = "Array")
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<PageQueryStream<AuditEventInstanceVO, AuditEventInstanceModel, AuditEventDao>> queryRQL(HttpServletRequest request) {
RestProcessResult<PageQueryStream<AuditEventInstanceVO, AuditEventInstanceModel, AuditEventDao>> result = new RestProcessResult<PageQueryStream<AuditEventInstanceVO, AuditEventInstanceModel, AuditEventDao>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
try {
if (!user.isAdmin()) {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
} else {
// Limit our results based on the fact that our permissions should be in the permissions strings
ASTNode root = parseRQLtoAST(request.getQueryString());
return result.createResponseEntity(getPageStream(root));
}
} catch (InvalidRQLRestException e) {
result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
return result.createResponseEntity();
}
}
return result.createResponseEntity();
}
use of com.wordnik.swagger.annotations.ApiOperation in project ma-modules-public by infiniteautomation.
the class DeviceNameController method deviceNamesByDataSourceXid.
@ApiOperation(value = "List device names by data source XID", response = String.class, responseContainer = "Set")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/by-data-source-xid/{xid}")
public ResponseEntity<Set<String>> deviceNamesByDataSourceXid(@PathVariable String xid, @RequestParam(value = "contains", required = false) String contains, HttpServletRequest request) {
RestProcessResult<Set<String>> result = new RestProcessResult<Set<String>>(HttpStatus.OK);
final User user = this.checkUser(request, result);
if (result.isOk()) {
DeviceAndPermissionCallback callback = new DeviceAndPermissionCallback(user);
if (contains != null) {
DataPointDao.instance.query(SELECT_DEVICENAME_JOIN_DATASOURCE + " WHERE ds.xid=? AND pt.deviceName LIKE ?", new Object[] { xid, "%" + contains + "%" }, DEVICE_AND_PERMISSION_MAPPER, callback);
} else {
DataPointDao.instance.query(SELECT_DEVICENAME_JOIN_DATASOURCE + " WHERE ds.xid=?", new Object[] { xid }, DEVICE_AND_PERMISSION_MAPPER, callback);
}
return result.createResponseEntity(callback.results);
}
return result.createResponseEntity();
}
Aggregations