Search in sources :

Example 16 with User

use of com.djrapitops.plan.delivery.domain.auth.User in project Plan by plan-player-analytics.

the class LoginResolver method getUser.

public User getUser(Request request) {
    URIQuery form = RequestBodyConverter.formBody(request);
    URIQuery query = request.getQuery();
    String username = getUser(form, query);
    String password = getPassword(form, query);
    User user = dbSystem.getDatabase().query(WebUserQueries.fetchUser(username)).orElseThrow(() -> new WebUserAuthException(FailReason.USER_PASS_MISMATCH));
    boolean correctPass = user.doesPasswordMatch(password);
    if (!correctPass) {
        throw new WebUserAuthException(FailReason.USER_PASS_MISMATCH);
    }
    return user;
}
Also used : User(com.djrapitops.plan.delivery.domain.auth.User) WebUserAuthException(com.djrapitops.plan.exceptions.WebUserAuthException) URIQuery(com.djrapitops.plan.delivery.web.resolver.request.URIQuery)

Example 17 with User

use of com.djrapitops.plan.delivery.domain.auth.User in project Plan by plan-player-analytics.

the class WebUserQueries method fetchUser.

public static Query<Optional<User>> fetchUser(String username) {
    String sql = SELECT + '*' + FROM + SecurityTable.TABLE_NAME + LEFT_JOIN + UsersTable.TABLE_NAME + " on " + SecurityTable.LINKED_TO + "=" + UsersTable.USER_UUID + WHERE + SecurityTable.USERNAME + "=? LIMIT 1";
    return new QueryStatement<Optional<User>>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setString(1, username);
        }

        @Override
        public Optional<User> processResults(ResultSet set) throws SQLException {
            if (set.next()) {
                String linkedTo = set.getString(UsersTable.USER_NAME);
                UUID linkedToUUID = linkedTo != null ? UUID.fromString(set.getString(SecurityTable.LINKED_TO)) : null;
                String passwordHash = set.getString(SecurityTable.SALT_PASSWORD_HASH);
                int permissionLevel = set.getInt(SecurityTable.PERMISSION_LEVEL);
                List<String> permissions = WebUser.getPermissionsForLevel(permissionLevel);
                return Optional.of(new User(username, linkedTo != null ? linkedTo : "console", linkedToUUID, passwordHash, permissionLevel, permissions));
            }
            return Optional.empty();
        }
    };
}
Also used : User(com.djrapitops.plan.delivery.domain.auth.User) WebUser(com.djrapitops.plan.delivery.domain.WebUser) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Example 18 with User

use of com.djrapitops.plan.delivery.domain.auth.User in project Plan by plan-player-analytics.

the class WebUserQueries method fetchActiveCookies.

public static Query<Map<String, User>> fetchActiveCookies() {
    String sql = SELECT + '*' + FROM + CookieTable.TABLE_NAME + INNER_JOIN + SecurityTable.TABLE_NAME + " on " + CookieTable.TABLE_NAME + '.' + CookieTable.WEB_USERNAME + '=' + SecurityTable.TABLE_NAME + '.' + SecurityTable.USERNAME + LEFT_JOIN + UsersTable.TABLE_NAME + " on " + SecurityTable.LINKED_TO + "=" + UsersTable.USER_UUID + WHERE + CookieTable.EXPIRES + ">?";
    return new QueryStatement<Map<String, User>>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setLong(1, System.currentTimeMillis());
        }

        @Override
        public Map<String, User> processResults(ResultSet set) throws SQLException {
            Map<String, User> usersByCookie = new HashMap<>();
            while (set.next()) {
                String cookie = set.getString(CookieTable.COOKIE);
                User user = extractUser(set);
                usersByCookie.put(cookie, user);
            }
            return usersByCookie;
        }
    };
}
Also used : User(com.djrapitops.plan.delivery.domain.auth.User) WebUser(com.djrapitops.plan.delivery.domain.WebUser) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Example 19 with User

use of com.djrapitops.plan.delivery.domain.auth.User in project Plan by plan-player-analytics.

the class WebUserQueries method fetchUser.

public static Query<Optional<User>> fetchUser(UUID linkedToUUID) {
    String sql = SELECT + '*' + FROM + SecurityTable.TABLE_NAME + LEFT_JOIN + UsersTable.TABLE_NAME + " on " + SecurityTable.LINKED_TO + "=" + UsersTable.USER_UUID + WHERE + SecurityTable.LINKED_TO + "=? LIMIT 1";
    return new QueryStatement<Optional<User>>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            if (linkedToUUID == null) {
                statement.setNull(1, Types.VARCHAR);
            } else {
                statement.setString(1, linkedToUUID.toString());
            }
        }

        @Override
        public Optional<User> processResults(ResultSet set) throws SQLException {
            if (set.next()) {
                String username = set.getString(SecurityTable.USERNAME);
                String linkedTo = set.getString(UsersTable.USER_NAME);
                String passwordHash = set.getString(SecurityTable.SALT_PASSWORD_HASH);
                int permissionLevel = set.getInt(SecurityTable.PERMISSION_LEVEL);
                List<String> permissions = WebUser.getPermissionsForLevel(permissionLevel);
                return Optional.of(new User(username, linkedTo != null ? linkedTo : "console", linkedToUUID, passwordHash, permissionLevel, permissions));
            }
            return Optional.empty();
        }
    };
}
Also used : User(com.djrapitops.plan.delivery.domain.auth.User) WebUser(com.djrapitops.plan.delivery.domain.WebUser) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Aggregations

User (com.djrapitops.plan.delivery.domain.auth.User)19 WebUser (com.djrapitops.plan.delivery.domain.WebUser)9 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 Database (com.djrapitops.plan.storage.database.Database)5 QueryStatement (com.djrapitops.plan.storage.database.queries.QueryStatement)5 RegisterWebUserTransaction (com.djrapitops.plan.storage.database.transactions.commands.RegisterWebUserTransaction)5 File (java.io.File)4 PlanConfig (com.djrapitops.plan.settings.config.PlanConfig)3 UUID (java.util.UUID)3 BeforeAll (org.junit.jupiter.api.BeforeAll)3 PluginMockComponent (utilities.mocks.PluginMockComponent)3 WebUserAuthException (com.djrapitops.plan.exceptions.WebUserAuthException)2 DBOpException (com.djrapitops.plan.exceptions.database.DBOpException)2 DBSystem (com.djrapitops.plan.storage.database.DBSystem)2 RemoveWebUserTransaction (com.djrapitops.plan.storage.database.transactions.commands.RemoveWebUserTransaction)2 PassEncryptUtil (com.djrapitops.plan.utilities.PassEncryptUtil)2 Collections (java.util.Collections)2 Test (org.junit.jupiter.api.Test)2 Arguments (com.djrapitops.plan.commands.use.Arguments)1