2020-05-12 01:26:09 +00:00
|
|
|
const { Model } = require('flitter-orm')
|
|
|
|
|
|
|
|
class ClientModel extends Model {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models', 'configs']
|
|
|
|
}
|
|
|
|
|
|
|
|
static get schema() {
|
|
|
|
return {
|
|
|
|
name: String,
|
|
|
|
user_id: String,
|
|
|
|
active: { type: Boolean, default: true },
|
|
|
|
last_invocation: Date,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async create({ name, uid, password }) {
|
|
|
|
const User = this.prototype.models.get('auth:User')
|
|
|
|
const user = new User({
|
|
|
|
first_name: name,
|
|
|
|
last_name: '(LDAP Agent)',
|
2020-10-19 04:27:23 +00:00
|
|
|
uid: uid.toLowerCase(),
|
2020-05-12 01:26:09 +00:00
|
|
|
roles: ['ldap_client'],
|
|
|
|
})
|
|
|
|
|
|
|
|
await user.reset_password(password, 'create')
|
|
|
|
await user.save()
|
|
|
|
|
|
|
|
const client = new this({
|
|
|
|
name,
|
|
|
|
user_id: user.id,
|
|
|
|
})
|
|
|
|
|
|
|
|
await client.save()
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2020-05-17 04:55:08 +00:00
|
|
|
async invoke() {
|
|
|
|
this.last_invocation = new Date
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:26:09 +00:00
|
|
|
async user() {
|
|
|
|
const User = this.models.get('auth:User')
|
|
|
|
return User.findById(this.user_id)
|
|
|
|
}
|
|
|
|
|
2020-05-17 04:55:08 +00:00
|
|
|
async application() {
|
|
|
|
const Application = this.models.get('Application')
|
|
|
|
return Application.findOne({ active: true, ldap_client_ids: this.id })
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:26:09 +00:00
|
|
|
async to_api() {
|
|
|
|
const User = this.models.get('auth:User')
|
|
|
|
const user = await User.findById(this.user_id)
|
|
|
|
const role_permissions = user.roles.map(x => this.configs.get('auth.roles')[x])
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
user_id: user.id,
|
2020-10-19 04:27:23 +00:00
|
|
|
uid: user.uid.toLowerCase(),
|
2020-05-12 01:26:09 +00:00
|
|
|
last_invocation: this.last_invocation,
|
|
|
|
permissions: [...user.permissions, ...role_permissions],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = ClientModel
|