Skip to content

Commit

Permalink
1014 #30 domain 新增做完了
Browse files Browse the repository at this point in the history
  • Loading branch information
Pulipuli Chen committed Oct 15, 2019
1 parent 7e78ab6 commit c9ee5ec
Show file tree
Hide file tree
Showing 22 changed files with 435 additions and 93 deletions.
50 changes: 50 additions & 0 deletions app/Controllers/Http/Admin/Domain.js
@@ -0,0 +1,50 @@
'use strict'

const DomainModel = use('App/Models/Domain')

const Config = use('Config')

class Domain {
async list ({request, auth}) {
await auth.checkGlobalAdmin()
//await auth.checkAdmin()

const {page = 1} = request.all()

const limit = Config.get('view.itemsPerPage')
const offset = (page - 1) * limit

let domains = await DomainModel
.offset(offset)
.limit(offset)
.fetch()

let count = await DomainModel.getCount()
let maxPage = Math.ceil(count / limit)

return {
domains,
maxPage
}
}

async add ({request, auth}) {
await auth.checkGlobalAdmin()

const data = request.all()

const domain = new DomainModel()
domain.domain = data.domain
if (data.title !== '') {
domain.title = data.title
}
if (data.config !== '') {
domain.config = data.config
}
await domain.save()

return 1
}
}

module.exports = Domain
3 changes: 2 additions & 1 deletion app/Controllers/Http/Admin/MaterialAsset.js
Expand Up @@ -5,7 +5,8 @@ const Drive = use('Drive')
const Helpers = use('Helpers')
const fs = require('fs')

const ItemsInPage = 10
const Config = use('Config')
const ItemsInPage = Config.get('view.itemsPerPage')

const Domain = use('App/Models/Domain')

Expand Down
20 changes: 20 additions & 0 deletions app/Helpers/AuthHelper.js
@@ -0,0 +1,20 @@
'use strict'

const ADMIN_ROLES = [
'global_admin',
'domain_admin',
]

const { HttpException } = use('@adonisjs/generic-exceptions')

let AuthHelper = {
chackAdmin: async function (auth) {
let user = await auth.getUser()
let role = user.role
if (ADMIN_ROLES.indexOf(role) === -1) {
throw new HttpException(`You don't have permisssion to access.`, 403)
}
}
}

module.exports = AuthHelper
41 changes: 41 additions & 0 deletions app/Middleware/AuthAdminCheck.js
@@ -0,0 +1,41 @@
'use strict'

const ADMIN_ROLES = [
'global_admin',
'domain_admin',
]

const { HttpException } = use('@adonisjs/generic-exceptions')

class AuthAdminCheck {
async handle (data, next) {
let auth = data.auth
let isAdmin = false
let isGlobalAdmin = false
try {
let user = await auth.getUser()
let role = user.role
isAdmin = (ADMIN_ROLES.indexOf(role) > -1)
isGlobalAdmin = (role === 'global_admin')
}
catch (e) {}
auth.isAdmin = isAdmin
auth.isGlobalAdmin = isGlobalAdmin

auth.checkAdmin = () => {
if (isAdmin === false) {
throw new HttpException(`You don't have permisssion to access.`, 403)
}
}

auth.checkGlobalAdmin = () => {
if (isGlobalAdmin === false) {
throw new HttpException(`You don't have permisssion to access.`, 403)
}
}

await next()
}
}

module.exports = AuthAdminCheck
2 changes: 1 addition & 1 deletion app/Models/Domain.js
Expand Up @@ -11,7 +11,7 @@ class Domain extends Model {

this.addHook('afterCreate', async (instance) => {
//instance.title = await
await this._crawlTitleFromURL(instance)
await instance._crawlTitleFromURL(instance)
})
}

Expand Down
5 changes: 5 additions & 0 deletions config/view.js
@@ -0,0 +1,5 @@
'use strict'

module.exports = {
itemsPerPage: 10
}
4 changes: 2 additions & 2 deletions database/migrations/1503248427884_domain_schema.js
Expand Up @@ -7,8 +7,8 @@ class DomainSchema extends Schema {
up () {
this.create('domains', (table) => {
table.increments()
table.string('domain', 254).notNullable()
table.text('title').notNullable().defaultTo('')
table.string('domain', 254).notNullable().unique()
table.text('title')
table.json('config')
table.timestamps()
})
Expand Down

0 comments on commit c9ee5ec

Please sign in to comment.