Flesh out users OU (works with Gitea simple LDAP now!!)
This commit is contained in:
@@ -2,7 +2,15 @@ const { Model } = require('flitter-orm')
|
||||
const ImplementationError = require('libflitter/errors/ImplementationError')
|
||||
|
||||
class LDAPBase extends Model {
|
||||
toLDAP() {
|
||||
static async ldap_directory() {
|
||||
return this.find({ldap_visible: true})
|
||||
}
|
||||
|
||||
get dn() {
|
||||
throw new ImplementationError()
|
||||
}
|
||||
|
||||
to_ldap() {
|
||||
throw new ImplementationError()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
const AuthUser = require('flitter-auth/model/User')
|
||||
const LDAP = require('ldapjs')
|
||||
|
||||
const ActiveScope = require('../scopes/ActiveScope')
|
||||
|
||||
/*
|
||||
* Auth user model. This inherits fields and methods from the default
|
||||
@@ -7,21 +10,61 @@ const AuthUser = require('flitter-auth/model/User')
|
||||
*/
|
||||
class User extends AuthUser {
|
||||
static get services() {
|
||||
return [...super.services, 'auth']
|
||||
return [...super.services, 'auth', 'ldap_server', 'ldap_dn_format']
|
||||
}
|
||||
|
||||
static get schema() {
|
||||
return {...super.schema, ...{
|
||||
// other schema fields here
|
||||
first_name: String,
|
||||
last_name: String,
|
||||
email: String,
|
||||
ldap_visible: {type: Boolean, default: true},
|
||||
active: {type: Boolean, default: true},
|
||||
}}
|
||||
}
|
||||
|
||||
static scopes = [
|
||||
new ActiveScope({})
|
||||
]
|
||||
|
||||
static async ldap_directory() {
|
||||
return this.find({ldap_visible: true})
|
||||
}
|
||||
|
||||
// Prefer soft delete because of the active scope
|
||||
async delete() {
|
||||
this.active = false
|
||||
await this.save()
|
||||
}
|
||||
|
||||
async check_password(password) {
|
||||
return this.get_provider().check_user_auth(this, password)
|
||||
}
|
||||
|
||||
get_provider() {
|
||||
return this.auth.get_provider(this.provider)
|
||||
to_ldap() {
|
||||
const ldap_data = {
|
||||
uid: this.uid,
|
||||
uuid: this.uuid,
|
||||
cn: this.first_name,
|
||||
sn: this.last_name,
|
||||
gecos: `${this.first_name} ${this.last_name}`,
|
||||
mail: this.email,
|
||||
objectClass: 'inetOrgPerson',
|
||||
dn: this.dn.format(this.ldap_dn_format),
|
||||
}
|
||||
|
||||
const addl_data = JSON.parse(this.data)
|
||||
for ( const key in addl_data ) {
|
||||
if ( !addl_data.hasOwnProperty(key) || !key.startsWith('ldap_') ) continue
|
||||
ldap_data[`data${key.substr(4)}`] = `${addl_data[key]}`
|
||||
}
|
||||
|
||||
return ldap_data
|
||||
}
|
||||
|
||||
get dn() {
|
||||
return LDAP.parseDN(`uid=${this.uid},${this.ldap_server.auth_dn().format(this.ldap_dn_format)}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
13
app/models/scopes/ActiveScope.js
Normal file
13
app/models/scopes/ActiveScope.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const { Scope } = require('flitter-orm')
|
||||
|
||||
/**
|
||||
* A flitter-orm scope that enables soft-deletion by an active key.
|
||||
* @extends {module:flitter-orm/src/model/Scope~Scope}
|
||||
*/
|
||||
class ActiveScope extends Scope {
|
||||
async filter(to_filter) {
|
||||
return to_filter.equal('active', true)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = ActiveScope
|
||||
Reference in New Issue
Block a user