Search in sources :

Example 1 with SSingleProjectAuthorization

use of org.bimserver.interfaces.objects.SSingleProjectAuthorization 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);
    }
}
Also used : User(org.bimserver.models.store.User) DatabaseSession(org.bimserver.database.DatabaseSession) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) SAuthorization(org.bimserver.interfaces.objects.SAuthorization) Authorization(org.bimserver.models.store.Authorization) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) Project(org.bimserver.models.store.Project) InternalServicePluginConfiguration(org.bimserver.models.store.InternalServicePluginConfiguration) UserException(org.bimserver.shared.exceptions.UserException) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) OAuthAuthorizationCode(org.bimserver.models.store.OAuthAuthorizationCode) SOAuthAuthorizationCode(org.bimserver.interfaces.objects.SOAuthAuthorizationCode)

Aggregations

BimserverDatabaseException (org.bimserver.BimserverDatabaseException)1 DatabaseSession (org.bimserver.database.DatabaseSession)1 SAuthorization (org.bimserver.interfaces.objects.SAuthorization)1 SOAuthAuthorizationCode (org.bimserver.interfaces.objects.SOAuthAuthorizationCode)1 SRunServiceAuthorization (org.bimserver.interfaces.objects.SRunServiceAuthorization)1 SSingleProjectAuthorization (org.bimserver.interfaces.objects.SSingleProjectAuthorization)1 Authorization (org.bimserver.models.store.Authorization)1 InternalServicePluginConfiguration (org.bimserver.models.store.InternalServicePluginConfiguration)1 OAuthAuthorizationCode (org.bimserver.models.store.OAuthAuthorizationCode)1 Project (org.bimserver.models.store.Project)1 RunServiceAuthorization (org.bimserver.models.store.RunServiceAuthorization)1 SingleProjectAuthorization (org.bimserver.models.store.SingleProjectAuthorization)1 User (org.bimserver.models.store.User)1 ServerException (org.bimserver.shared.exceptions.ServerException)1 UserException (org.bimserver.shared.exceptions.UserException)1