use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.
the class UsersService method getByIdViaCache.
public User getByIdViaCache(int id) {
return dao.doInTransaction((tx) -> {
String username = dao.getXidById(id);
if (username == null) {
throw new NotFoundException();
}
User user = get(username);
if (user.getId() != id) {
throw new IllegalStateException("User was updated while retrieving from cache");
}
return user;
});
}
use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.
the class PublishedPointImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
PublishedPointVO vo = null;
PublisherVO publisherVO = null;
DataPointVO dataPointVO = null;
if (StringUtils.isBlank(xid)) {
xid = service.generateUniqueXid();
} else {
try {
vo = service.get(xid);
} catch (NotFoundException e) {
}
}
if (vo == null) {
String pubXid = json.getString("publisherXid");
try {
publisherVO = publisherService.get(pubXid);
} catch (NotFoundException e) {
addFailureMessage("emport.publishedPoint.badPublisherReference", xid);
return;
}
String dpXid = json.getString("dataPointXid");
try {
dataPointVO = dataPointService.get(dpXid);
} catch (NotFoundException e) {
addFailureMessage("emport.publishedPoint.badDataPointReference", xid);
return;
}
vo = publisherVO.getDefinition().createPublishedPointVO(publisherVO, dataPointVO);
vo.setXid(xid);
}
if (vo != null) {
try {
// The VO was found or successfully created. Finish reading it in.
ctx.getReader().readInto(vo, json);
boolean isnew = vo.isNew();
if (Common.runtimeManager.getLifecycleState() == ILifecycleState.RUNNING) {
if (isnew) {
service.insert(vo);
} else {
service.update(vo.getId(), vo);
}
addSuccessMessage(isnew, "emport.publishedPoint.prefix", xid);
} else {
addFailureMessage("emport.publishedPoint.runtimeManagerNotRunning", xid);
}
} catch (ValidationException e) {
setValidationMessages(e.getValidationResult(), "emport.publishedPoint.prefix", xid);
} catch (TranslatableJsonException e) {
addFailureMessage("emport.publishedPoint.prefix", xid, e.getMsg());
} catch (JsonException e) {
addFailureMessage("emport.publishedPoint.prefix", xid, getJsonExceptionMessage(e));
}
}
}
use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.
the class RoleImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
String name = json.getString("name");
RoleVO vo = null;
if (StringUtils.isBlank(xid)) {
xid = service.generateUniqueXid();
} else {
try {
vo = service.get(xid);
} catch (NotFoundException e) {
}
}
if (vo == null) {
vo = new RoleVO(Common.NEW_ID, xid, name);
}
try {
// Read into the VO to get all properties
ctx.getReader().readInto(vo, json);
boolean isnew = vo.getId() == Common.NEW_ID;
if (isnew) {
service.insert(vo);
} else {
service.update(vo.getId(), vo);
}
addSuccessMessage(isnew, "emport.role.prefix", xid);
} catch (ValidationException e) {
setValidationMessages(e.getValidationResult(), "emport.role.prefix", xid);
} catch (JsonException e) {
addFailureMessage("emport.role.prefix", xid, getJsonExceptionMessage(e));
}
}
use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.
the class MangoTokenAuthenticationProvider method authenticate.
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (!(authentication instanceof BearerAuthenticationToken)) {
return null;
}
String bearerToken = (String) authentication.getCredentials();
User user;
Jws<Claims> jws;
try {
jws = tokenAuthenticationService.parse(bearerToken);
user = tokenAuthenticationService.verify(jws);
} catch (ExpiredJwtException e) {
throw new CredentialsExpiredException("JWT token expired", e);
} catch (UnsupportedJwtException | MalformedJwtException | IllegalArgumentException e) {
// assume that this is not a JWT, allow the next AuthenticationProvider to process it
return null;
} catch (SignatureException | MissingClaimException | IncorrectClaimException e) {
throw new BadCredentialsException("JWT signature verification error or claim incorrect", e);
} catch (NotFoundException e) {
throw new BadCredentialsException("Invalid username", e);
} catch (Exception e) {
throw new InternalAuthenticationServiceException("Error authenticating with JWT token", e);
}
userDetailsChecker.check(user);
if (log.isDebugEnabled()) {
log.debug("Successfully authenticated user using JWT token, header: " + jws.getHeader() + ", body: " + jws.getBody());
}
return new JwtAuthentication(user, bearerToken, jws, user.getAuthorities());
}
use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.
the class EmailAddressVerificationService method generateToken.
/**
* Generate an email verification token
*
* @param userToUpdate Optional, may be null
* @param expirationDate Optional, may be null
*/
public String generateToken(String emailAddress, User userToUpdate, Date expirationDate) {
if (userToUpdate == null) {
this.ensurePublicRegistrationEnabled();
}
long verificationTime = System.currentTimeMillis();
if (expirationDate == null) {
int expiryDuration = this.systemSettings.getIntValue(EXPIRY_SYSTEM_SETTING);
expirationDate = new Date(verificationTime + expiryDuration * 1000L);
}
runAs.runAs(runAs.systemSuperadmin(), () -> {
try {
// see if a different user is already using this address
User existingUser = this.usersService.getUserByEmail(emailAddress);
if (userToUpdate == null || existingUser.getId() != userToUpdate.getId()) {
throw new EmailAddressInUseException(existingUser);
}
} catch (NotFoundException e) {
// no existing user using this email address, proceed
}
});
JwtBuilder builder = this.newToken(emailAddress, expirationDate);
builder.setIssuedAt(new Date(verificationTime));
if (userToUpdate != null) {
this.usersService.ensureEditPermission(Common.getUser(), userToUpdate);
builder.claim(USER_ID_CLAIM, userToUpdate.getId());
builder.claim(USERNAME_CLAIM, userToUpdate.getUsername());
}
return this.sign(builder);
}
Aggregations