You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CoreID/app/models/ldap/Client.model.js

69 lines
1.8 KiB

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)',
uid: uid.toLowerCase(),
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
}
async invoke() {
this.last_invocation = new Date
}
async user() {
const User = this.models.get('auth:User')
return User.findById(this.user_id)
}
async application() {
const Application = this.models.get('Application')
return Application.findOne({ active: true, ldap_client_ids: this.id })
}
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,
uid: user.uid.toLowerCase(),
last_invocation: this.last_invocation,
permissions: [...user.permissions, ...role_permissions],
}
}
}
module.exports = exports = ClientModel