]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/installer.ts
Support roles with rights and add moderator role
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
index 10b74b85fd50cb8b2334f52716d4e4fc2522b21d..07747234187ed65efe667a2268b790494bc5e4aa 100644 (file)
@@ -1,18 +1,20 @@
 import * as passwordGenerator from 'password-generator'
-import * as Promise from 'bluebird'
+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'
-
-function installApplication () {
-  return db.sequelize.sync()
-    .then(() => removeCacheDirectories())
-    .then(() => createDirectoriesIfNotExist())
-    .then(() => createCertsIfNotExist())
-    .then(() => createOAuthClientIfNotExist())
-    .then(() => createOAuthAdminIfNotExist())
+import { createUserAuthorAndChannel } from '../lib'
+import { UserRole } from '../../shared'
+
+async function installApplication () {
+  await db.sequelize.sync()
+  await removeCacheDirectories()
+  await createDirectoriesIfNotExist()
+  await createCertsIfNotExist()
+  await createOAuthClientIfNotExist()
+  await createOAuthAdminIfNotExist()
 }
 
 // ---------------------------------------------------------------------------
@@ -26,13 +28,13 @@ export {
 function removeCacheDirectories () {
   const cacheDirectories = CACHE.DIRECTORIES
 
-  const tasks = []
+  const tasks: Bluebird<any>[] = []
 
   // Cache directories
-  Object.keys(cacheDirectories).forEach(key => {
+  for (const key of Object.keys(cacheDirectories)) {
     const dir = cacheDirectories[key]
     tasks.push(rimrafPromise(dir))
-  })
+  }
 
   return Promise.all(tasks)
 }
@@ -42,86 +44,83 @@ function createDirectoriesIfNotExist () {
   const cacheDirectories = CACHE.DIRECTORIES
 
   const tasks = []
-  Object.keys(storage).forEach(key => {
+  for (const key of Object.keys(storage)) {
     const dir = storage[key]
     tasks.push(mkdirpPromise(dir))
-  })
+  }
 
   // Cache directories
-  Object.keys(cacheDirectories).forEach(key => {
+  for (const key of Object.keys(cacheDirectories)) {
     const dir = cacheDirectories[key]
     tasks.push(mkdirpPromise(dir))
-  })
+  }
 
   return Promise.all(tasks)
 }
 
-function createOAuthClientIfNotExist () {
-  return clientsExist(db.OAuthClient).then(exist => {
-    // Nothing to do, clients already exist
-    if (exist === true) return undefined
-
-    logger.info('Creating a default OAuth Client.')
+async function createOAuthClientIfNotExist () {
+  const exist = await clientsExist(db.OAuthClient)
+  // Nothing to do, clients already exist
+  if (exist === true) return undefined
 
-    const id = passwordGenerator(32, false, /[a-z0-9]/)
-    const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
-    const client = db.OAuthClient.build({
-      clientId: id,
-      clientSecret: secret,
-      grants: [ 'password', 'refresh_token' ],
-      redirectUris: null
-    })
+  logger.info('Creating a default OAuth Client.')
 
-    return client.save().then(createdClient => {
-      logger.info('Client id: ' + createdClient.clientId)
-      logger.info('Client secret: ' + createdClient.clientSecret)
-
-      return undefined
-    })
+  const id = passwordGenerator(32, false, /[a-z0-9]/)
+  const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
+  const client = db.OAuthClient.build({
+    clientId: id,
+    clientSecret: secret,
+    grants: [ 'password', 'refresh_token' ],
+    redirectUris: null
   })
-}
 
-function createOAuthAdminIfNotExist () {
-  return usersExist(db.User).then(exist => {
-    // Nothing to do, users already exist
-    if (exist === true) return undefined
+  const createdClient = await client.save()
+  logger.info('Client id: ' + createdClient.clientId)
+  logger.info('Client secret: ' + createdClient.clientSecret)
 
-    logger.info('Creating the administrator.')
+  return undefined
+}
 
-    const username = 'root'
-    const role = USER_ROLES.ADMIN
-    const email = CONFIG.ADMIN.EMAIL
-    const createOptions: { validate?: boolean } = {}
-    let password = ''
+async function createOAuthAdminIfNotExist () {
+  const exist = await usersExist(db.User)
+  // Nothing to do, users already exist
+  if (exist === true) return undefined
 
-    // Do not generate a random password for tests
-    if (process.env.NODE_ENV === 'test') {
-      password = 'test'
+  logger.info('Creating the administrator.')
 
-      if (process.env.NODE_APP_INSTANCE) {
-        password += process.env.NODE_APP_INSTANCE
-      }
+  const username = 'root'
+  const role = UserRole.ADMINISTRATOR
+  const email = CONFIG.ADMIN.EMAIL
+  let validatePassword = true
+  let password = ''
 
-      // Our password is weak so do not validate it
-      createOptions.validate = false
-    } else {
-      password = passwordGenerator(8, true)
-    }
+  // Do not generate a random password for tests
+  if (process.env.NODE_ENV === 'test') {
+    password = 'test'
 
-    const userData = {
-      username,
-      email,
-      password,
-      role,
-      videoQuota: -1
+    if (process.env.NODE_APP_INSTANCE) {
+      password += process.env.NODE_APP_INSTANCE
     }
 
-    return db.User.create(userData, createOptions).then(createdUser => {
-      logger.info('Username: ' + username)
-      logger.info('User password: ' + password)
-
-      logger.info('Creating Application table.')
-      return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
-    })
-  })
+    // Our password is weak so do not validate it
+    validatePassword = false
+  } else {
+    password = passwordGenerator(8, true)
+  }
+
+  const userData = {
+    username,
+    email,
+    password,
+    role,
+    videoQuota: -1
+  }
+  const user = db.User.build(userData)
+
+  await createUserAuthorAndChannel(user, validatePassword)
+  logger.info('Username: ' + username)
+  logger.info('User password: ' + password)
+
+  logger.info('Creating Application table.')
+  await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
 }