From 954605a804da399317ca62afa2fb9244afa11ebf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 27 Oct 2017 16:55:03 +0200 Subject: Support roles with rights and add moderator role --- server/initializers/constants.ts | 13 +------- server/initializers/installer.ts | 5 +-- server/initializers/migrations/0085-user-role.ts | 39 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 server/initializers/migrations/0085-user-role.ts (limited to 'server/initializers') diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 1581a3195..6dc9737d2 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -5,7 +5,6 @@ import { join } from 'path' import { root, isTestInstance } from '../helpers/core-utils' import { - UserRole, VideoRateType, RequestEndpoint, RequestVideoEventType, @@ -16,7 +15,7 @@ import { // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 80 +const LAST_MIGRATION_VERSION = 85 // --------------------------------------------------------------------------- @@ -283,7 +282,6 @@ const JOB_STATES: { [ id: string ]: JobState } = { } // How many maximum jobs we fetch from the database per cycle const JOBS_FETCH_LIMIT_PER_CYCLE = 10 -const JOBS_CONCURRENCY = 1 // 1 minutes let JOBS_FETCHING_INTERVAL = 60000 @@ -334,13 +332,6 @@ const CACHE = { // --------------------------------------------------------------------------- -const USER_ROLES: { [ id: string ]: UserRole } = { - ADMIN: 'admin', - USER: 'user' -} - -// --------------------------------------------------------------------------- - const OPENGRAPH_AND_OEMBED_COMMENT = '' // --------------------------------------------------------------------------- @@ -367,7 +358,6 @@ export { EMBED_SIZE, FRIEND_SCORE, JOB_STATES, - JOBS_CONCURRENCY, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL, LAST_MIGRATION_VERSION, @@ -401,7 +391,6 @@ export { STATIC_MAX_AGE, STATIC_PATHS, THUMBNAILS_SIZE, - USER_ROLES, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index 4c04290fc..077472341 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -2,10 +2,11 @@ import * as passwordGenerator from 'password-generator' import * as Bluebird from 'bluebird' import { database as db } from './database' -import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants' +import { CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants' import { clientsExist, usersExist } from './checker' import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers' import { createUserAuthorAndChannel } from '../lib' +import { UserRole } from '../../shared' async function installApplication () { await db.sequelize.sync() @@ -88,7 +89,7 @@ async function createOAuthAdminIfNotExist () { logger.info('Creating the administrator.') const username = 'root' - const role = USER_ROLES.ADMIN + const role = UserRole.ADMINISTRATOR const email = CONFIG.ADMIN.EMAIL let validatePassword = true let password = '' diff --git a/server/initializers/migrations/0085-user-role.ts b/server/initializers/migrations/0085-user-role.ts new file mode 100644 index 000000000..e67c5ca24 --- /dev/null +++ b/server/initializers/migrations/0085-user-role.ts @@ -0,0 +1,39 @@ +import * as Sequelize from 'sequelize' +import * as uuidv4 from 'uuid/v4' + +async function up (utils: { + transaction: Sequelize.Transaction, + queryInterface: Sequelize.QueryInterface, + sequelize: Sequelize.Sequelize, + db: any +}): Promise { + const q = utils.queryInterface + + await q.renameColumn('Users', 'role', 'oldRole') + + const data = { + type: Sequelize.INTEGER, + allowNull: true + } + await q.addColumn('Users', 'role', data) + + let query = 'UPDATE "Users" SET "role" = 0 WHERE "oldRole" = \'admin\'' + await utils.sequelize.query(query) + + query = 'UPDATE "Users" SET "role" = 2 WHERE "oldRole" = \'user\'' + await utils.sequelize.query(query) + + data.allowNull = false + await q.changeColumn('Users', 'role', data) + + await q.removeColumn('Users', 'oldRole') +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} -- cgit v1.2.3