Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pulipuli Chen
committed
Oct 15, 2019
1 parent
7e78ab6
commit c9ee5ec
Showing
22 changed files
with
435 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
itemsPerPage: 10 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.