use of org.bimserver.interfaces.objects.SRunServiceAuthorization in project BIMserver by opensourceBIM.
the class OAuthServiceImpl method authorize.
@Override
public String authorize(Long oAuthServerOid, SAuthorization authorization) throws ServerException, UserException {
try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
if (authorization instanceof SSingleProjectAuthorization) {
User user = session.get(getCurrentUser().getOid(), OldQuery.getDefault());
SSingleProjectAuthorization sSingleProjectAuthorization = (SSingleProjectAuthorization) authorization;
SingleProjectAuthorization singleProjectAuthorization = session.create(SingleProjectAuthorization.class);
Project project = session.get(sSingleProjectAuthorization.getProjectId(), OldQuery.getDefault());
if (project == null) {
throw new UserException("No project found with poid " + sSingleProjectAuthorization.getProjectId());
}
singleProjectAuthorization.setProject(project);
OAuthAuthorizationCode code = session.create(OAuthAuthorizationCode.class);
org.bimserver.webservices.authorization.Authorization auth = new org.bimserver.webservices.authorization.SingleProjectAuthorization(getBimServer(), user.getOid(), project.getOid());
String asHexToken = auth.asHexToken(getBimServer().getEncryptionKey());
code.setCode(asHexToken);
code.setOauthServer(session.get(oAuthServerOid, OldQuery.getDefault()));
code.setAuthorization(singleProjectAuthorization);
code.setUser(user);
user.getOAuthIssuedAuthorizationCodes().add(code);
session.store(user);
session.store(singleProjectAuthorization);
session.commit();
return code.getCode();
} else if (authorization instanceof SRunServiceAuthorization) {
SRunServiceAuthorization serviceAuthorization = (SRunServiceAuthorization) authorization;
User user = session.get(getCurrentUser().getOid(), OldQuery.getDefault());
RunServiceAuthorization runServiceAuth = session.create(RunServiceAuthorization.class);
InternalServicePluginConfiguration conf = session.get(serviceAuthorization.getServiceId(), OldQuery.getDefault());
if (conf == null) {
throw new UserException("No service found with soid " + serviceAuthorization.getServiceId());
}
runServiceAuth.setService(conf);
OAuthAuthorizationCode code = session.create(OAuthAuthorizationCode.class);
org.bimserver.webservices.authorization.Authorization auth = new org.bimserver.webservices.authorization.RunServiceAuthorization(getBimServer(), user.getOid(), conf.getOid());
String asHexToken = auth.asHexToken(getBimServer().getEncryptionKey());
code.setCode(asHexToken);
code.setOauthServer(session.get(oAuthServerOid, OldQuery.getDefault()));
code.setAuthorization(runServiceAuth);
code.setUser(user);
user.getOAuthIssuedAuthorizationCodes().add(code);
session.store(user);
session.store(code);
session.store(runServiceAuth);
session.commit();
return code.getCode();
} else {
throw new UserException("Unimplemented type of authorization " + authorization.getClass().getSimpleName());
}
} catch (Exception e) {
return handleException(e);
}
}
Aggregations