Skip to content

Commit

Permalink
fix(admin): fix role editing
Browse files Browse the repository at this point in the history
  • Loading branch information
emirotin committed Aug 31, 2018
1 parent 888bd21 commit bbe501b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
6 changes: 2 additions & 4 deletions packages/core/src/database/tables/server-wide/auth-roles.ts
Expand Up @@ -6,16 +6,14 @@ export default class AuthRolesTable extends Table {
async bootstrap() {
await this.knex.createTableIfNotExists(this.name, table => {
table.increments('id')
table
.string('name')
.unique()
.notNullable()
table.string('name').notNullable()
table.text('description')
table.json('rules').notNullable()
table
.integer('team')
.references('auth_teams.id')
.onDelete('CASCADE')
table.unique(['team', 'name'])
table.timestamps()
})
}
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/services/auth/teams-service.ts
Expand Up @@ -186,11 +186,11 @@ export default class TeamService {
.then()
}

async updateTeamRole(teamId: number, roleId: number, role: AuthRole) {
async updateTeamRole(teamId: number, roleId: number, role: Partial<AuthRole>) {
const dbRole = await this.knex(ROLES_TABLE)
.select('name')
.where({ id: roleId, team: teamId })
.then<AuthRole[]>(res => res)
.then<AuthRoleDb[]>(res => res)
.get(0)

if (!dbRole) {
Expand All @@ -201,9 +201,14 @@ export default class TeamService {
throw new InvalidOperationError("You can't edit the owner role")
}

const patchRole: Partial<AuthRoleDb> = _.pick(role, 'description')
if ('rules' in role) {
patchRole.rules = JSON.stringify(role.rules)
}

return this.knex(ROLES_TABLE)
.where('id', roleId)
.update(_.pick(role, 'description', 'rules'))
.update(patchRole)
.then()
}

Expand Down

0 comments on commit bbe501b

Please sign in to comment.