Search in sources :

Example 11 with User

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

the class AccessControlTest method setUpClass.

@BeforeAll
static void setUpClass(@TempDir Path tempDir) throws Exception {
    File file = tempDir.resolve("TestCert.p12").toFile();
    File testCert = TestResources.getTestResourceFile("TestCert.p12", ConfigUpdater.class);
    Files.copy(testCert.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
    String absolutePath = file.getAbsolutePath();
    PluginMockComponent component = new PluginMockComponent(tempDir);
    system = component.getPlanSystem();
    PlanConfig config = system.getConfigSystem().getConfig();
    config.set(WebserverSettings.CERTIFICATE_PATH, absolutePath);
    config.set(WebserverSettings.CERTIFICATE_KEYPASS, "test");
    config.set(WebserverSettings.CERTIFICATE_STOREPASS, "test");
    config.set(WebserverSettings.CERTIFICATE_ALIAS, "test");
    config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
    system.enable();
    User userLevel0 = new User("test0", "console", null, PassEncryptUtil.createHash("testPass"), 0, Collections.emptyList());
    User userLevel1 = new User("test1", "console", null, PassEncryptUtil.createHash("testPass"), 1, Collections.emptyList());
    User userLevel2 = new User("test2", TestConstants.PLAYER_ONE_NAME, TestConstants.PLAYER_ONE_UUID, PassEncryptUtil.createHash("testPass"), 2, Collections.emptyList());
    User userLevel100 = new User("test100", "console", null, PassEncryptUtil.createHash("testPass"), 100, Collections.emptyList());
    system.getDatabaseSystem().getDatabase().executeTransaction(new RegisterWebUserTransaction(userLevel0));
    system.getDatabaseSystem().getDatabase().executeTransaction(new RegisterWebUserTransaction(userLevel1));
    system.getDatabaseSystem().getDatabase().executeTransaction(new RegisterWebUserTransaction(userLevel2));
    system.getDatabaseSystem().getDatabase().executeTransaction(new RegisterWebUserTransaction(userLevel100));
    system.getDatabaseSystem().getDatabase().executeTransaction(new PlayerRegisterTransaction(TestConstants.PLAYER_ONE_UUID, () -> 0L, TestConstants.PLAYER_ONE_NAME));
    system.getDatabaseSystem().getDatabase().executeTransaction(new StoreServerInformationTransaction(new Server(TestConstants.SERVER_UUID, TestConstants.SERVER_NAME, address, TestConstants.VERSION)));
    Caller caller = system.getExtensionService().register(new ExtensionsDatabaseTest.PlayerExtension()).orElseThrow(AssertionError::new);
    caller.updatePlayerData(TestConstants.PLAYER_ONE_UUID, TestConstants.PLAYER_ONE_NAME);
    address = "https://localhost:" + TEST_PORT_NUMBER;
    cookieLevel0 = login(address, userLevel0.getUsername());
    cookieLevel1 = login(address, userLevel1.getUsername());
    cookieLevel2 = login(address, userLevel2.getUsername());
    cookieLevel100 = login(address, userLevel100.getUsername());
}
Also used : User(com.djrapitops.plan.delivery.domain.auth.User) RegisterWebUserTransaction(com.djrapitops.plan.storage.database.transactions.commands.RegisterWebUserTransaction) Server(com.djrapitops.plan.identification.Server) Caller(com.djrapitops.plan.extension.Caller) StoreServerInformationTransaction(com.djrapitops.plan.storage.database.transactions.StoreServerInformationTransaction) PlayerRegisterTransaction(com.djrapitops.plan.storage.database.transactions.events.PlayerRegisterTransaction) PluginMockComponent(utilities.mocks.PluginMockComponent) File(java.io.File) PlanConfig(com.djrapitops.plan.settings.config.PlanConfig) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 12 with User

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

the class ActiveCookieStoreTest method createActiveCookieStore.

@BeforeEach
void createActiveCookieStore() {
    DBSystem dbSystem = Mockito.mock(DBSystem.class);
    Database db = Mockito.mock(Database.class);
    when(dbSystem.getDatabase()).thenReturn(db);
    underTest = new ActiveCookieStore(Mockito.mock(ActiveCookieExpiryCleanupTask.class), Mockito.mock(PlanConfig.class), dbSystem, Mockito.mock(Processing.class), new TestPluginLogger());
    user = new User(TestConstants.PLAYER_ONE_NAME, "console", null, PassEncryptUtil.createHash("testPass"), 0, WebUser.getPermissionsForLevel(0));
}
Also used : DBSystem(com.djrapitops.plan.storage.database.DBSystem) User(com.djrapitops.plan.delivery.domain.auth.User) WebUser(com.djrapitops.plan.delivery.domain.WebUser) Database(com.djrapitops.plan.storage.database.Database) TestPluginLogger(utilities.TestPluginLogger) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 13 with User

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

the class LargeStoreQueries method storeAllPlanWebUsers.

public static Executable storeAllPlanWebUsers(Collection<User> users) {
    if (users == null || users.isEmpty())
        return Executable.empty();
    return new ExecBatchStatement(SecurityTable.INSERT_STATEMENT) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            for (User user : users) {
                statement.setString(1, user.getUsername());
                if (user.getLinkedToUUID() == null) {
                    statement.setNull(2, Types.VARCHAR);
                } else {
                    statement.setString(2, user.getLinkedToUUID().toString());
                }
                statement.setString(3, user.getPasswordHash());
                statement.setInt(4, user.getPermissionLevel());
                statement.addBatch();
            }
        }
    };
}
Also used : User(com.djrapitops.plan.delivery.domain.auth.User) PreparedStatement(java.sql.PreparedStatement) ExecBatchStatement(com.djrapitops.plan.storage.database.transactions.ExecBatchStatement)

Example 14 with User

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

the class RegistrationCommands method registerUsingCode.

public void registerUsingCode(CMDSender sender, String code) {
    UUID linkedToUUID = sender.getUUID().orElse(null);
    Optional<User> user = RegistrationBin.register(code, linkedToUUID);
    if (user.isPresent()) {
        registerUser(user.get(), sender, getPermissionLevel(sender));
    } else {
        throw new IllegalArgumentException(locale.getString(FailReason.USER_INFORMATION_NOT_FOUND));
    }
}
Also used : User(com.djrapitops.plan.delivery.domain.auth.User) UUID(java.util.UUID)

Example 15 with User

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

the class RegistrationCommands method onUnregister.

public void onUnregister(String mainCommand, CMDSender sender, Arguments arguments) {
    Optional<String> givenUsername = arguments.get(0).filter(arg -> sender.hasPermission(Permissions.UNREGISTER_OTHER));
    Database database = dbSystem.getDatabase();
    UUID playerUUID = sender.getUUID().orElse(null);
    String username;
    if (!givenUsername.isPresent() && playerUUID != null) {
        Optional<User> found = database.query(WebUserQueries.fetchUser(playerUUID));
        if (!found.isPresent()) {
            throw new IllegalArgumentException(locale.getString(CommandLang.USER_NOT_LINKED));
        }
        username = found.get().getUsername();
    } else if (!givenUsername.isPresent()) {
        throw new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ONE_ARG, "<" + locale.getString(HelpLang.ARG_USERNAME) + ">"));
    } else {
        username = givenUsername.get();
    }
    Optional<User> found = database.query(WebUserQueries.fetchUser(username));
    if (!found.isPresent()) {
        throw new IllegalArgumentException(locale.getString(FailReason.USER_DOES_NOT_EXIST));
    }
    User presentUser = found.get();
    boolean ownsTheUser = Objects.equals(playerUUID, presentUser.getLinkedToUUID());
    if (!(ownsTheUser || sender.hasPermission(Permissions.UNREGISTER_OTHER.getPerm()))) {
        throw new IllegalArgumentException(locale.getString(CommandLang.USER_NOT_LINKED));
    }
    if (sender.supportsChatEvents()) {
        sender.buildMessage().addPart(colors.getMainColor() + locale.getString(CommandLang.CONFIRM_UNREGISTER, presentUser.getUsername(), presentUser.getLinkedTo())).newLine().addPart(colors.getTertiaryColor() + locale.getString(CommandLang.CONFIRM)).addPart("§2§l[\u2714]").command("/" + mainCommand + " accept").hover(locale.getString(CommandLang.CONFIRM_ACCEPT)).addPart(" ").addPart("§4§l[\u2718]").command("/" + mainCommand + " cancel").hover(locale.getString(CommandLang.CONFIRM_DENY)).send();
    } else {
        sender.buildMessage().addPart(colors.getMainColor() + locale.getString(CommandLang.CONFIRM_UNREGISTER, presentUser.getUsername(), presentUser.getLinkedTo())).newLine().addPart(colors.getTertiaryColor() + locale.getString(CommandLang.CONFIRM)).addPart("§a/" + mainCommand + " accept").addPart(" ").addPart("§c/" + mainCommand + " cancel").send();
    }
    confirmation.confirm(sender, choice -> {
        if (Boolean.TRUE.equals(choice)) {
            try {
                sender.send(colors.getMainColor() + locale.getString(CommandLang.UNREGISTER, presentUser.getUsername()));
                database.executeTransaction(new RemoveWebUserTransaction(username)).get();
                ActiveCookieStore.removeUserCookie(username);
                sender.send(locale.getString(CommandLang.PROGRESS_SUCCESS));
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (ExecutionException e) {
                errorLogger.warn(e, ErrorContext.builder().related("unregister command", sender, sender.getPlayerName().orElse("console"), arguments).build());
            }
        } else {
            sender.send(colors.getMainColor() + locale.getString(CommandLang.CONFIRM_CANCELLED_UNREGISTER, presentUser.getUsername()));
        }
    });
}
Also used : User(com.djrapitops.plan.delivery.domain.auth.User) Database(com.djrapitops.plan.storage.database.Database) RemoveWebUserTransaction(com.djrapitops.plan.storage.database.transactions.commands.RemoveWebUserTransaction) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException)

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