Search in sources :

Example 1 with Roles

use of com.infiniteautomation.mango.db.tables.Roles in project ma-core-public by infiniteautomation.

the class H2DatabaseTest method test1AutoIncrement.

@Test
public void test1AutoIncrement() throws SQLException {
    DSLContext context = Common.getBean(DatabaseProxy.class).getContext();
    Roles r = Roles.ROLES;
    context.insertInto(r, r.id, r.xid, r.name).values(10, "xid", "name").execute();
    context.insertInto(r, r.xid, r.name).values("test", "test").execute();
    RoleVO role = Common.getBean(RoleDao.class).getByXid("test");
    assertEquals(11, role.getId());
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) DSLContext(org.jooq.DSLContext) Roles(com.infiniteautomation.mango.db.tables.Roles) Test(org.junit.Test)

Example 2 with Roles

use of com.infiniteautomation.mango.db.tables.Roles in project ma-core-public by infiniteautomation.

the class MySQLDatabaseTest method test1AutoIncrement.

@Test
public void test1AutoIncrement() throws SQLException {
    DSLContext context = Common.getBean(DatabaseProxy.class).getContext();
    Roles r = Roles.ROLES;
    context.insertInto(r, r.id, r.xid, r.name).values(10, "xid", "name").execute();
    context.insertInto(r, r.xid, r.name).values("test", "test").execute();
    RoleVO role = Common.getBean(RoleDao.class).getByXid("test");
    assertEquals(11, role.getId());
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) DSLContext(org.jooq.DSLContext) Roles(com.infiniteautomation.mango.db.tables.Roles) Test(org.junit.Test)

Example 3 with Roles

use of com.infiniteautomation.mango.db.tables.Roles in project ma-core-public by infiniteautomation.

the class PostgresDatabaseTest method test1AutoIncrement.

@Test
public void test1AutoIncrement() throws SQLException {
    DSLContext context = Common.getBean(DatabaseProxy.class).getContext();
    Roles r = Roles.ROLES;
    context.insertInto(r, r.id, r.xid, r.name).values(10, "xid", "name").execute();
    context.insertInto(r, r.xid, r.name).values("test", "test").execute();
    RoleVO role = Common.getBean(RoleDao.class).getByXid("test");
    assertNotEquals(11, role.getId());
    assertEquals(4, role.getId());
    String sequence = r.getName() + "_" + r.id.getName() + "_seq";
    context.alterSequence(sequence).restartWith(20).execute();
    context.insertInto(r, r.xid, r.name).values("test2", "test2").execute();
    role = Common.getBean(RoleDao.class).getByXid("test2");
    assertEquals(20, role.getId());
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) DSLContext(org.jooq.DSLContext) Roles(com.infiniteautomation.mango.db.tables.Roles) Test(org.junit.Test)

Example 4 with Roles

use of com.infiniteautomation.mango.db.tables.Roles in project ma-core-public by infiniteautomation.

the class DatabaseProxy method initializeCoreDatabase.

/**
 * Inserts and updates data for a new installation
 */
default void initializeCoreDatabase(DSLContext context) {
    SystemSettings ss = SystemSettings.SYSTEM_SETTINGS;
    Roles r = Roles.ROLES;
    Users u = Users.USERS;
    UserRoleMappings urm = UserRoleMappings.USER_ROLE_MAPPINGS;
    RoleInheritance ri = RoleInheritance.ROLE_INHERITANCE;
    Permissions permissions = Permissions.PERMISSIONS;
    context.insertInto(ss, ss.settingName, ss.settingValue).values(SystemSettingsDao.NEW_INSTANCE, BaseDao.boolToChar(true)).values(SystemSettingsDao.DATABASE_SCHEMA_VERSION, Integer.toString(Common.getDatabaseSchemaVersion())).execute();
    Translations translations = Common.getTranslations();
    context.insertInto(r, r.id, r.xid, r.name).values(PermissionHolder.SUPERADMIN_ROLE.getId(), PermissionHolder.SUPERADMIN_ROLE.getXid(), translations.translate("roles.superadmin")).values(PermissionHolder.USER_ROLE.getId(), PermissionHolder.USER_ROLE.getXid(), translations.translate("roles.user")).values(PermissionHolder.ANONYMOUS_ROLE.getId(), PermissionHolder.ANONYMOUS_ROLE.getXid(), translations.translate("roles.anonymous")).execute();
    // Fix next sequence value for postgres
    if (getType().getDialect() == SQLDialect.POSTGRES) {
        String sequence = r.getName() + "_" + r.id.getName() + "_seq";
        context.alterSequence(sequence).restartWith(4).execute();
    }
    context.insertInto(ri, ri.roleId, ri.inheritedRoleId).values(PermissionHolder.SUPERADMIN_ROLE.getId(), PermissionHolder.USER_ROLE.getId()).values(PermissionHolder.USER_ROLE.getId(), PermissionHolder.ANONYMOUS_ROLE.getId()).execute();
    // create superadmin only permission, with no minterm mappings
    int adminOnlyPermissionId = context.insertInto(permissions).defaultValues().returningResult(permissions.id).fetchOptional().orElseThrow(IllegalStateException::new).get(permissions.id);
    if (Common.envProps.getBoolean("initialize.admin.create")) {
        long createdTs = System.currentTimeMillis();
        String defaultPassword = Common.envProps.getProperty("initialize.admin.password");
        long passwordChangeTs = defaultPassword.equals("admin") ? createdTs : createdTs + 1;
        int adminId = context.insertInto(u).set(u.name, translations.translate("users.defaultAdministratorName")).set(u.username, Common.envProps.getProperty("initialize.admin.username")).set(u.password, Common.encrypt(defaultPassword)).set(u.email, Common.envProps.getProperty("initialize.admin.email")).set(u.phone, "").set(u.disabled, BaseDao.boolToChar(false)).set(u.lastLogin, 0L).set(u.homeUrl, "/ui/administration/home").set(u.receiveAlarmEmails, AlarmLevels.IGNORE.value()).set(u.receiveOwnAuditEvents, BaseDao.boolToChar(false)).set(u.muted, BaseDao.boolToChar(true)).set(u.tokenVersion, 1).set(u.passwordVersion, 1).set(u.passwordChangeTimestamp, passwordChangeTs).set(u.sessionExpirationOverride, BaseDao.boolToChar(false)).set(u.createdTs, createdTs).set(u.readPermissionId, adminOnlyPermissionId).set(u.editPermissionId, adminOnlyPermissionId).returningResult(u.id).fetchOptional().orElseThrow(IllegalStateException::new).get(u.id);
        context.insertInto(urm, urm.userId, urm.roleId).values(adminId, PermissionHolder.SUPERADMIN_ROLE.getId()).values(adminId, PermissionHolder.USER_ROLE.getId()).execute();
    }
}
Also used : UserRoleMappings(com.infiniteautomation.mango.db.tables.UserRoleMappings) RoleInheritance(com.infiniteautomation.mango.db.tables.RoleInheritance) Permissions(com.infiniteautomation.mango.db.tables.Permissions) Roles(com.infiniteautomation.mango.db.tables.Roles) Users(com.infiniteautomation.mango.db.tables.Users) SystemSettings(com.infiniteautomation.mango.db.tables.SystemSettings) Translations(com.serotonin.m2m2.i18n.Translations)

Aggregations

Roles (com.infiniteautomation.mango.db.tables.Roles)4 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)3 RoleVO (com.serotonin.m2m2.vo.role.RoleVO)3 DSLContext (org.jooq.DSLContext)3 Test (org.junit.Test)3 Permissions (com.infiniteautomation.mango.db.tables.Permissions)1 RoleInheritance (com.infiniteautomation.mango.db.tables.RoleInheritance)1 SystemSettings (com.infiniteautomation.mango.db.tables.SystemSettings)1 UserRoleMappings (com.infiniteautomation.mango.db.tables.UserRoleMappings)1 Users (com.infiniteautomation.mango.db.tables.Users)1 Translations (com.serotonin.m2m2.i18n.Translations)1