diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml index 933bb285ca..dc2d4bd237 100644 --- a/locales/ja-JP.yml +++ b/locales/ja-JP.yml @@ -938,7 +938,12 @@ _role: name: "ロール名" description: "ロールの説明" permission: "ロールの権限" - descriptionOfType: "モデレーターは基本的なモデレーションに関する操作を行えます。\n管理者はインスタンスの全ての設定を変更できます。" + descriptionOfPermission: "モデレーターは基本的なモデレーションに関する操作を行えます。\n管理者はインスタンスの全ての設定を変更できます。" + assignTarget: "アサインターゲット" + descriptionOfAssignTarget: "マニュアルは誰がこのロールに含まれるかを手動で管理します。\nコンディショナルは条件を設定し、それに合致するユーザーが自動で含まれるようになります。" + manual: "マニュアル" + conditional: "コンディショナル" + condition: "条件" isPublic: "ロールを公開" descriptionOfIsPublic: "ロールにアサインされたユーザーを誰でも見ることができます。また、ユーザーのプロフィールでこのロールが表示されます。" options: "オプション" @@ -953,6 +958,14 @@ _role: canPublicNote: "パブリック投稿の許可" driveCapacity: "ドライブ容量" antennaMax: "アンテナの作成可能数" + _condition: + isLocal: "ローカルユーザー" + isRemote: "リモートユーザー" + createdLessThan: "アカウント作成から~以内" + createdMoreThan: "アカウント作成から~経過" + and: "~かつ~" + or: "~または~" + not: "~ではない" _sensitiveMediaDetection: description: "機械学習を使って自動でセンシティブなメディアを検出し、モデレーションに役立てることができます。サーバーの負荷が少し増えます。" diff --git a/packages/backend/migration/1673570377815-RoleConditional.js b/packages/backend/migration/1673570377815-RoleConditional.js new file mode 100644 index 0000000000..11ae4f00c6 --- /dev/null +++ b/packages/backend/migration/1673570377815-RoleConditional.js @@ -0,0 +1,15 @@ +export class RoleConditional1673570377815 { + name = 'RoleConditional1673570377815' + + async up(queryRunner) { + await queryRunner.query(`CREATE TYPE "public"."role_target_enum" AS ENUM('manual', 'conditional')`); + await queryRunner.query(`ALTER TABLE "role" ADD "target" "public"."role_target_enum" NOT NULL DEFAULT 'manual'`); + await queryRunner.query(`ALTER TABLE "role" ADD "condFormula" jsonb NOT NULL DEFAULT '{}'`); + } + + async down(queryRunner) { + await queryRunner.query(`ALTER TABLE "role" DROP COLUMN "condFormula"`); + await queryRunner.query(`ALTER TABLE "role" DROP COLUMN "target"`); + await queryRunner.query(`DROP TYPE "public"."role_target_enum"`); + } +} diff --git a/packages/backend/src/core/RoleService.ts b/packages/backend/src/core/RoleService.ts index 6ce7f431ca..3183adb369 100644 --- a/packages/backend/src/core/RoleService.ts +++ b/packages/backend/src/core/RoleService.ts @@ -7,6 +7,9 @@ import type { CacheableLocalUser, CacheableUser, ILocalUser, User } from '@/mode import { DI } from '@/di-symbols.js'; import { bindThis } from '@/decorators.js'; import { MetaService } from '@/core/MetaService.js'; +import { UserCacheService } from '@/core/UserCacheService.js'; +import { RoleCondFormulaValue } from '@/models/entities/Role.js'; +import { UserEntityService } from '@/core/entities/UserEntityService.js'; import type { OnApplicationShutdown } from '@nestjs/common'; export type RoleOptions = { @@ -44,6 +47,8 @@ export class RoleService implements OnApplicationShutdown { private roleAssignmentsRepository: RoleAssignmentsRepository, private metaService: MetaService, + private userCacheService: UserCacheService, + private userEntityService: UserEntityService, ) { //this.onMessage = this.onMessage.bind(this); @@ -111,12 +116,49 @@ export class RoleService implements OnApplicationShutdown { } } + @bindThis + private evalCond(user: User, value: RoleCondFormulaValue): boolean { + try { + switch (value.type) { + case 'and': { + return value.values.every(v => this.evalCond(user, v)); + } + case 'or': { + return value.values.some(v => this.evalCond(user, v)); + } + case 'not': { + return !this.evalCond(user, value.value); + } + case 'isLocal': { + return this.userEntityService.isLocalUser(user); + } + case 'isRemote': { + return this.userEntityService.isRemoteUser(user); + } + case 'createdLessThan': { + return user.createdAt.getTime() > (Date.now() - (value.sec * 1000)); + } + case 'createdMoreThan': { + return user.createdAt.getTime() < (Date.now() - (value.sec * 1000)); + } + default: + return false; + } + } catch (err) { + // TODO: log error + return false; + } + } + @bindThis public async getUserRoles(userId: User['id']) { const assigns = await this.roleAssignmentByUserIdCache.fetch(userId, () => this.roleAssignmentsRepository.findBy({ userId })); const assignedRoleIds = assigns.map(x => x.roleId); const roles = await this.rolesCache.fetch(null, () => this.rolesRepository.findBy({})); - return roles.filter(r => assignedRoleIds.includes(r.id)); + const assignedRoles = roles.filter(r => assignedRoleIds.includes(r.id)); + const user = roles.some(r => r.target === 'conditional') ? await this.userCacheService.findById(userId) : null; + const matchedCondRoles = roles.filter(r => r.target === 'conditional' && this.evalCond(user!, r.condFormula)); + return [...assignedRoles, ...matchedCondRoles]; } @bindThis diff --git a/packages/backend/src/core/entities/RoleEntityService.ts b/packages/backend/src/core/entities/RoleEntityService.ts index 22c4cdff81..27e34a649a 100644 --- a/packages/backend/src/core/entities/RoleEntityService.ts +++ b/packages/backend/src/core/entities/RoleEntityService.ts @@ -55,6 +55,8 @@ export class RoleEntityService { name: role.name, description: role.description, color: role.color, + target: role.target, + condFormula: role.condFormula, isPublic: role.isPublic, isAdministrator: role.isAdministrator, isModerator: role.isModerator, diff --git a/packages/backend/src/models/entities/Role.ts b/packages/backend/src/models/entities/Role.ts index 34dbc2ce41..f7b4edc9e7 100644 --- a/packages/backend/src/models/entities/Role.ts +++ b/packages/backend/src/models/entities/Role.ts @@ -1,6 +1,48 @@ import { Entity, Index, JoinColumn, Column, PrimaryColumn, ManyToOne } from 'typeorm'; import { id } from '../id.js'; +type CondFormulaValueAnd = { + type: 'and'; + values: RoleCondFormulaValue[]; +}; + +type CondFormulaValueOr = { + type: 'or'; + values: RoleCondFormulaValue[]; +}; + +type CondFormulaValueNot = { + type: 'not'; + value: RoleCondFormulaValue; +}; + +type CondFormulaValueIsLocal = { + type: 'isLocal'; +}; + +type CondFormulaValueIsRemote = { + type: 'isRemote'; +}; + +type CondFormulaValueCreatedLessThan = { + type: 'createdLessThan'; + sec: number; +}; + +type CondFormulaValueCreatedMoreThan = { + type: 'createdMoreThan'; + sec: number; +}; + +export type RoleCondFormulaValue = + CondFormulaValueAnd | + CondFormulaValueOr | + CondFormulaValueNot | + CondFormulaValueIsLocal | + CondFormulaValueIsRemote | + CondFormulaValueCreatedLessThan | + CondFormulaValueCreatedMoreThan; + @Entity() export class Role { @PrimaryColumn(id()) @@ -36,6 +78,17 @@ export class Role { }) public color: string | null; + @Column('enum', { + enum: ['manual', 'conditional'], + default: 'manual', + }) + public target: 'manual' | 'conditional'; + + @Column('jsonb', { + default: { }, + }) + public condFormula: RoleCondFormulaValue; + @Column('boolean', { default: false, }) diff --git a/packages/backend/src/server/api/endpoints/admin/roles/create.ts b/packages/backend/src/server/api/endpoints/admin/roles/create.ts index b04188fac6..a9216a6386 100644 --- a/packages/backend/src/server/api/endpoints/admin/roles/create.ts +++ b/packages/backend/src/server/api/endpoints/admin/roles/create.ts @@ -19,6 +19,8 @@ export const paramDef = { name: { type: 'string' }, description: { type: 'string' }, color: { type: 'string', nullable: true }, + target: { type: 'string' }, + condFormula: { type: 'object' }, isPublic: { type: 'boolean' }, isModerator: { type: 'boolean' }, isAdministrator: { type: 'boolean' }, @@ -31,6 +33,8 @@ export const paramDef = { 'name', 'description', 'color', + 'target', + 'condFormula', 'isPublic', 'isModerator', 'isAdministrator', @@ -60,6 +64,8 @@ export default class extends Endpoint { name: ps.name, description: ps.description, color: ps.color, + target: ps.target, + condFormula: ps.condFormula, isPublic: ps.isPublic, isAdministrator: ps.isAdministrator, isModerator: ps.isModerator, diff --git a/packages/backend/src/server/api/endpoints/admin/roles/update.ts b/packages/backend/src/server/api/endpoints/admin/roles/update.ts index 7d97d68e14..4ca5124eda 100644 --- a/packages/backend/src/server/api/endpoints/admin/roles/update.ts +++ b/packages/backend/src/server/api/endpoints/admin/roles/update.ts @@ -27,6 +27,8 @@ export const paramDef = { name: { type: 'string' }, description: { type: 'string' }, color: { type: 'string', nullable: true }, + target: { type: 'string' }, + condFormula: { type: 'object' }, isPublic: { type: 'boolean' }, isModerator: { type: 'boolean' }, isAdministrator: { type: 'boolean' }, @@ -40,6 +42,8 @@ export const paramDef = { 'name', 'description', 'color', + 'target', + 'condFormula', 'isPublic', 'isModerator', 'isAdministrator', @@ -69,6 +73,8 @@ export default class extends Endpoint { name: ps.name, description: ps.description, color: ps.color, + target: ps.target, + condFormula: ps.condFormula, isPublic: ps.isPublic, isModerator: ps.isModerator, isAdministrator: ps.isAdministrator, diff --git a/packages/frontend/src/pages/admin/RolesEditorFormula.vue b/packages/frontend/src/pages/admin/RolesEditorFormula.vue new file mode 100644 index 0000000000..76ba639277 --- /dev/null +++ b/packages/frontend/src/pages/admin/RolesEditorFormula.vue @@ -0,0 +1,129 @@ + + + + + {{ i18n.ts._role._condition.isLocal }} + {{ i18n.ts._role._condition.isRemote }} + {{ i18n.ts._role._condition.createdLessThan }} + {{ i18n.ts._role._condition.createdMoreThan }} + {{ i18n.ts._role._condition.and }} + {{ i18n.ts._role._condition.or }} + {{ i18n.ts._role._condition.not }} + + + + + + + + + + + + valuesItemUpdated(updated)"/> + + + + {{ i18n.ts.add }} + + + + + + + + sec + + + + + + + diff --git a/packages/frontend/src/pages/admin/roles.editor.vue b/packages/frontend/src/pages/admin/roles.editor.vue index b8e45cda50..f584c5c8bf 100644 --- a/packages/frontend/src/pages/admin/roles.editor.vue +++ b/packages/frontend/src/pages/admin/roles.editor.vue @@ -15,12 +15,26 @@ {{ i18n.ts._role.permission }} - + {{ i18n.ts.normalUser }} {{ i18n.ts.moderator }} {{ i18n.ts.administrator }} + + {{ i18n.ts._role.assignTarget }} + + {{ i18n.ts._role.manual }} + {{ i18n.ts._role.conditional }} + + + + {{ i18n.ts._role.condition }} + + + + + {{ i18n.ts._role.options }} @@ -107,7 +121,9 @@