2020-02-08 03:08:24 +00:00
|
|
|
const AuthUser = require('flitter-auth/model/User')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Auth user model. This inherits fields and methods from the default
|
|
|
|
* flitter-auth/model/User model, however you can override methods and
|
|
|
|
* properties here as you need.
|
|
|
|
*/
|
|
|
|
class User extends AuthUser {
|
2020-02-08 18:36:42 +00:00
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models']
|
|
|
|
}
|
|
|
|
|
2020-02-08 03:08:24 +00:00
|
|
|
static get schema() {
|
|
|
|
return {...super.schema, ...{
|
|
|
|
// other schema fields here
|
2021-02-15 17:40:46 +00:00
|
|
|
full_name: String,
|
2020-10-20 15:12:25 +00:00
|
|
|
preferences: {
|
|
|
|
dark_mode: { type: Boolean, default: false },
|
2020-10-29 04:48:31 +00:00
|
|
|
auto_prefetch: { type: Boolean, default: false },
|
2020-10-20 15:12:25 +00:00
|
|
|
default_page: String,
|
2021-02-18 16:18:57 +00:00
|
|
|
bookmark_page_ids: [String],
|
2020-10-20 15:12:25 +00:00
|
|
|
},
|
2020-02-08 03:08:24 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2020-02-08 18:36:42 +00:00
|
|
|
async get_root_page() {
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
return Page.findOne({OrgUserId: this._id, ParentId: '0'})
|
|
|
|
}
|
|
|
|
|
2021-02-18 16:18:57 +00:00
|
|
|
async get_bookmarked_pages() {
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
const pages = await Page.find({ UUID: { $in: this.preferences.bookmark_page_ids || [] }})
|
|
|
|
|
|
|
|
const visible = []
|
|
|
|
for ( const page of pages ) {
|
|
|
|
if ( await page.is_accessible_by(this) ) {
|
|
|
|
visible.push(page)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return visible
|
|
|
|
}
|
|
|
|
|
2020-10-13 02:46:50 +00:00
|
|
|
async get_accessible_pages() {
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
const user_page = await this.get_root_page()
|
|
|
|
|
|
|
|
const user_pages = await user_page.visible_flat_children(this)
|
|
|
|
|
|
|
|
let view_pages = await Page.find({ shared_users_view: this._id })
|
|
|
|
for ( const page of view_pages ) {
|
|
|
|
view_pages = [...view_pages, ...(await page.visible_flat_children(this))]
|
|
|
|
}
|
|
|
|
|
|
|
|
let update_pages = await Page.find({ shared_users_update: this._id })
|
|
|
|
for ( const page of update_pages ) {
|
|
|
|
update_pages = [...update_pages, ...(await page.visible_flat_children(this))]
|
|
|
|
}
|
|
|
|
|
|
|
|
let manage_pages = await Page.find({ shared_users_manage: this._id })
|
|
|
|
for ( const page of manage_pages ) {
|
|
|
|
manage_pages = [...manage_pages, ...(await page.visible_flat_children(this))]
|
|
|
|
}
|
|
|
|
|
|
|
|
const all_pages = [...user_pages, ...view_pages, ...update_pages, ...manage_pages].filter(x => !x.virtual)
|
|
|
|
const uniq_page_obj = {}
|
|
|
|
for ( const page of all_pages ) {
|
|
|
|
uniq_page_obj[page.UUID] = page
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.values(uniq_page_obj)
|
|
|
|
}
|
|
|
|
|
2020-02-08 03:08:24 +00:00
|
|
|
// Other members and methods here
|
2020-11-11 18:23:31 +00:00
|
|
|
is_public_user() {
|
|
|
|
return false
|
|
|
|
}
|
2021-03-04 17:26:14 +00:00
|
|
|
|
|
|
|
async can(permission) {
|
|
|
|
if ( super.can(permission) ) return true
|
|
|
|
|
|
|
|
const PublicUserPermission = this.models.get('auth:PublicUserPermission')
|
|
|
|
return await PublicUserPermission.can(permission)
|
|
|
|
}
|
2021-03-16 21:50:01 +00:00
|
|
|
|
|
|
|
allow(permission, force = false){
|
|
|
|
// Need to check super.can, since this.can is async.
|
|
|
|
// This will exclude public user permissions, which is the behavior we want anyway.
|
|
|
|
if ( !super.can(permission) || (force && !this.permissions.includes(permission)) ) this.permissions.push(permission)
|
|
|
|
}
|
2020-02-08 03:08:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 17:26:14 +00:00
|
|
|
|
2020-02-08 03:08:24 +00:00
|
|
|
module.exports = exports = User
|