initial commit
This commit is contained in:
commit
4fc3bb0a01
202 changed files with 41729 additions and 0 deletions
52
app/Models/Dataset.ts
Normal file
52
app/Models/Dataset.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import {
|
||||
column,
|
||||
BaseModel,
|
||||
SnakeCaseNamingStrategy,
|
||||
// computed,
|
||||
manyToMany,
|
||||
ManyToMany,
|
||||
} from '@ioc:Adonis/Lucid/Orm';
|
||||
import { DateTime } from 'luxon';
|
||||
import Person from './Person';
|
||||
|
||||
|
||||
export default class Dataset extends BaseModel {
|
||||
public static namingStrategy = new SnakeCaseNamingStrategy();
|
||||
public static primaryKey = 'id';
|
||||
public static table = 'documents';
|
||||
public static selfAssignPrimaryKey = false;
|
||||
|
||||
@column({ isPrimary: true })
|
||||
public id: number;
|
||||
|
||||
@column({})
|
||||
public server_state: boolean;
|
||||
|
||||
@column({})
|
||||
public publisherName: string;
|
||||
|
||||
@column.dateTime({ columnName: 'embargo_date' })
|
||||
public EmbargoDate: DateTime;
|
||||
|
||||
@column({})
|
||||
public type: string;
|
||||
|
||||
|
||||
@column.dateTime({ columnName: 'server_date_published' })
|
||||
public ServerDatePublished: DateTime;
|
||||
|
||||
@column.dateTime({ autoCreate: true, columnName: 'created_at' })
|
||||
public createdAt: DateTime;
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
public updatedAt: DateTime;
|
||||
|
||||
@manyToMany(() => Person, {
|
||||
pivotForeignKey: 'document_id',
|
||||
pivotRelatedForeignKey: 'person_id',
|
||||
pivotTable: 'link_documents_persons',
|
||||
pivotColumns: ['role', 'sort_order', 'allow_email_contact']
|
||||
})
|
||||
public persons: ManyToMany<typeof Person>;
|
||||
|
||||
}
|
62
app/Models/File.ts
Normal file
62
app/Models/File.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import { DateTime } from 'luxon';
|
||||
import {
|
||||
column,
|
||||
BaseModel,
|
||||
hasMany, HasMany,
|
||||
// manyToMany,
|
||||
// ManyToMany,
|
||||
SnakeCaseNamingStrategy,
|
||||
} from '@ioc:Adonis/Lucid/Orm';
|
||||
import HashValue from './HashValue';
|
||||
|
||||
export default class File extends BaseModel {
|
||||
public static namingStrategy = new SnakeCaseNamingStrategy();
|
||||
public static primaryKey = 'id';
|
||||
public static table = 'document_files';
|
||||
public static selfAssignPrimaryKey = false;
|
||||
|
||||
@column({
|
||||
isPrimary: true,
|
||||
})
|
||||
public id: number;
|
||||
|
||||
@column({})
|
||||
public pathName: string;
|
||||
|
||||
@column()
|
||||
public label: string;
|
||||
|
||||
@column()
|
||||
public comment: string;
|
||||
|
||||
@column()
|
||||
public mimetype: string;
|
||||
|
||||
@column()
|
||||
public language: string;
|
||||
|
||||
@column()
|
||||
public fileSize: bigint;
|
||||
|
||||
@column()
|
||||
public visibleInOai: boolean;
|
||||
|
||||
@column()
|
||||
public sortOrder: number;
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime;
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
public updatedAt: DateTime;
|
||||
|
||||
// public function hashvalues()
|
||||
// {
|
||||
// return $this->hasMany(HashValue::class, 'file_id', 'id');
|
||||
// }
|
||||
|
||||
@hasMany(() => HashValue, {
|
||||
foreignKey: 'file_id',
|
||||
})
|
||||
public hashvalues: HasMany<typeof HashValue>;
|
||||
}
|
41
app/Models/HashValue.ts
Normal file
41
app/Models/HashValue.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import {
|
||||
column,
|
||||
BaseModel,
|
||||
belongsTo,
|
||||
BelongsTo,
|
||||
SnakeCaseNamingStrategy,
|
||||
} from '@ioc:Adonis/Lucid/Orm';
|
||||
import File from './File';
|
||||
|
||||
export default class HashValue extends BaseModel {
|
||||
public static namingStrategy = new SnakeCaseNamingStrategy();
|
||||
public static primaryKey = 'file_id, type';
|
||||
public static table = 'file_hashvalues';
|
||||
|
||||
// static get primaryKey () {
|
||||
// return 'type, value'
|
||||
// }
|
||||
|
||||
static get incrementing () {
|
||||
return false
|
||||
}
|
||||
|
||||
// @column({
|
||||
// isPrimary: true,
|
||||
// })
|
||||
// public id: number;
|
||||
|
||||
// Foreign key is still on the same model
|
||||
@column({})
|
||||
public file_id: number;
|
||||
|
||||
@column({})
|
||||
public type: string;
|
||||
|
||||
@column()
|
||||
public value: string;
|
||||
|
||||
@belongsTo(() => File)
|
||||
public file: BelongsTo<typeof File>
|
||||
|
||||
}
|
100
app/Models/Permission.ts
Normal file
100
app/Models/Permission.ts
Normal file
|
@ -0,0 +1,100 @@
|
|||
import {
|
||||
column,
|
||||
BaseModel,
|
||||
manyToMany,
|
||||
ManyToMany,
|
||||
SnakeCaseNamingStrategy,
|
||||
beforeUpdate,
|
||||
beforeCreate,
|
||||
} from '@ioc:Adonis/Lucid/Orm';
|
||||
import { DateTime } from 'luxon';
|
||||
import dayjs from 'dayjs';
|
||||
import Role from 'App/Models/Role';
|
||||
|
||||
export default class Permission extends BaseModel {
|
||||
public static namingStrategy = new SnakeCaseNamingStrategy();
|
||||
public static primaryKey = 'id';
|
||||
public static table = 'permissions';
|
||||
public static selfAssignPrimaryKey = false;
|
||||
|
||||
@column({
|
||||
isPrimary: true,
|
||||
})
|
||||
public id: number;
|
||||
|
||||
@column({})
|
||||
public role_id: number;
|
||||
|
||||
@column({})
|
||||
public display_name: string;
|
||||
|
||||
@column({})
|
||||
public name: string;
|
||||
|
||||
@column({})
|
||||
public description: string;
|
||||
|
||||
@column.dateTime({
|
||||
serialize: (value: Date | null) => {
|
||||
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
|
||||
},
|
||||
autoCreate: true,
|
||||
})
|
||||
public created_at: DateTime;
|
||||
|
||||
@column.dateTime({
|
||||
serialize: (value: Date | null) => {
|
||||
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
|
||||
},
|
||||
autoCreate: true,
|
||||
autoUpdate: true,
|
||||
})
|
||||
public updated_at: DateTime;
|
||||
|
||||
@beforeCreate()
|
||||
@beforeUpdate()
|
||||
public static async resetDate(role) {
|
||||
role.created_at = this.formatDateTime(role.created_at);
|
||||
role.updated_at = this.formatDateTime(role.updated_at);
|
||||
}
|
||||
|
||||
// public static boot() {
|
||||
// super.boot()
|
||||
|
||||
// this.before('create', async (_modelInstance) => {
|
||||
// _modelInstance.created_at = this.formatDateTime(_modelInstance.created_at)
|
||||
// _modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at)
|
||||
// })
|
||||
// this.before('update', async (_modelInstance) => {
|
||||
// _modelInstance.created_at = this.formatDateTime(_modelInstance.created_at)
|
||||
// _modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at)
|
||||
// })
|
||||
// }
|
||||
|
||||
private static formatDateTime(datetime) {
|
||||
let value = new Date(datetime);
|
||||
return datetime
|
||||
? value.getFullYear() +
|
||||
'-' +
|
||||
(value.getMonth() + 1) +
|
||||
'-' +
|
||||
value.getDate() +
|
||||
' ' +
|
||||
value.getHours() +
|
||||
':' +
|
||||
value.getMinutes() +
|
||||
':' +
|
||||
value.getSeconds()
|
||||
: datetime;
|
||||
}
|
||||
|
||||
// @belongsTo(() => Role)
|
||||
// public role: BelongsTo<typeof Role>;
|
||||
|
||||
@manyToMany(() => Role, {
|
||||
pivotForeignKey: 'permission_id',
|
||||
pivotRelatedForeignKey: 'role_id',
|
||||
pivotTable: 'role_has_permissions',
|
||||
})
|
||||
public roles: ManyToMany<typeof Role>;
|
||||
}
|
83
app/Models/Person.ts
Normal file
83
app/Models/Person.ts
Normal file
|
@ -0,0 +1,83 @@
|
|||
import {
|
||||
column,
|
||||
BaseModel,
|
||||
SnakeCaseNamingStrategy,
|
||||
computed,
|
||||
manyToMany,
|
||||
ManyToMany,
|
||||
} from '@ioc:Adonis/Lucid/Orm';
|
||||
import { DateTime } from 'luxon';
|
||||
import dayjs from 'dayjs';
|
||||
import Dataset from './Dataset';
|
||||
|
||||
export default class Person extends BaseModel {
|
||||
public static namingStrategy = new SnakeCaseNamingStrategy();
|
||||
public static primaryKey = 'id';
|
||||
public static table = 'persons';
|
||||
public static selfAssignPrimaryKey = false;
|
||||
|
||||
@column({
|
||||
isPrimary: true,
|
||||
})
|
||||
public id: number;
|
||||
|
||||
@column({})
|
||||
public academicTitle: string;
|
||||
|
||||
@column()
|
||||
public email: string;
|
||||
|
||||
@column({})
|
||||
public firstName: string;
|
||||
|
||||
@column({})
|
||||
public lastName: string;
|
||||
|
||||
@column({})
|
||||
public identifierOrcid: string;
|
||||
|
||||
@column({})
|
||||
public status: boolean;
|
||||
|
||||
@column({})
|
||||
public nameType: string;
|
||||
|
||||
@column.dateTime({
|
||||
serialize: (value: Date | null) => {
|
||||
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
|
||||
},
|
||||
autoCreate: true,
|
||||
})
|
||||
public registeredAt: DateTime;
|
||||
|
||||
@computed({
|
||||
serializeAs: 'name'
|
||||
})
|
||||
public get fullName() {
|
||||
return this.firstName + ' ' + this.lastName;
|
||||
}
|
||||
|
||||
@computed()
|
||||
public get progress(): number {
|
||||
return 50;
|
||||
}
|
||||
|
||||
@computed()
|
||||
public get created_at() {
|
||||
return '2023-02-17 08:45:56';
|
||||
}
|
||||
|
||||
@computed()
|
||||
public get datasetCount() {
|
||||
const stock = this.$extras.datasets_count //my pivot column name was "stock"
|
||||
return stock
|
||||
}
|
||||
|
||||
@manyToMany(() => Dataset, {
|
||||
pivotForeignKey: 'person_id',
|
||||
pivotRelatedForeignKey: 'document_id',
|
||||
pivotTable: 'link_documents_persons',
|
||||
pivotColumns: ['role', 'sort_order', 'allow_email_contact']
|
||||
})
|
||||
public datasets: ManyToMany<typeof Dataset>;
|
||||
}
|
105
app/Models/Role.ts
Normal file
105
app/Models/Role.ts
Normal file
|
@ -0,0 +1,105 @@
|
|||
import {
|
||||
column,
|
||||
BaseModel,
|
||||
SnakeCaseNamingStrategy,
|
||||
manyToMany,
|
||||
ManyToMany,
|
||||
beforeCreate,
|
||||
beforeUpdate,
|
||||
} from '@ioc:Adonis/Lucid/Orm';
|
||||
|
||||
import { DateTime } from 'luxon';
|
||||
// import moment from 'moment';
|
||||
import dayjs from 'dayjs';
|
||||
import User from './User';
|
||||
import Permission from 'App/Models/Permission';
|
||||
|
||||
export default class Role extends BaseModel {
|
||||
public static namingStrategy = new SnakeCaseNamingStrategy();
|
||||
public static primaryKey = 'id';
|
||||
public static table = 'roles';
|
||||
public static selfAssignPrimaryKey = false;
|
||||
|
||||
@column({
|
||||
isPrimary: true,
|
||||
})
|
||||
public id: number;
|
||||
|
||||
@column({})
|
||||
public display_name: string;
|
||||
|
||||
@column({})
|
||||
public name: string;
|
||||
|
||||
@column({})
|
||||
public description: string;
|
||||
|
||||
@column.dateTime({
|
||||
serialize: (value: Date | null) => {
|
||||
// return value ? moment(value).format('MMMM Do YYYY, HH:mm:ss') : value;
|
||||
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
|
||||
},
|
||||
autoCreate: true,
|
||||
})
|
||||
public created_at: DateTime;
|
||||
|
||||
@column.dateTime({
|
||||
serialize: (value: Date | null) => {
|
||||
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
|
||||
},
|
||||
autoCreate: true,
|
||||
autoUpdate: true
|
||||
})
|
||||
public updated_at: DateTime;
|
||||
|
||||
@beforeCreate()
|
||||
@beforeUpdate()
|
||||
public static async resetDate(role) {
|
||||
role.created_at = this.formatDateTime(role.created_at);
|
||||
role.updated_at = this.formatDateTime(role.updated_at);
|
||||
}
|
||||
|
||||
// public static boot() {
|
||||
// super.boot();
|
||||
|
||||
// this.before('create', async (_modelInstance) => {
|
||||
// _modelInstance.created_at = this.formatDateTime(_modelInstance.created_at);
|
||||
// _modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at);
|
||||
// });
|
||||
// this.before('update', async (_modelInstance) => {
|
||||
// _modelInstance.created_at = this.formatDateTime(_modelInstance.created_at);
|
||||
// _modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at);
|
||||
// });
|
||||
// }
|
||||
|
||||
private static formatDateTime(datetime) {
|
||||
let value = new Date(datetime);
|
||||
return datetime
|
||||
? value.getFullYear() +
|
||||
'-' +
|
||||
(value.getMonth() + 1) +
|
||||
'-' +
|
||||
value.getDate() +
|
||||
' ' +
|
||||
value.getHours() +
|
||||
':' +
|
||||
value.getMinutes() +
|
||||
':' +
|
||||
value.getSeconds()
|
||||
: datetime;
|
||||
}
|
||||
|
||||
@manyToMany(() => User, {
|
||||
pivotForeignKey: 'role_id',
|
||||
pivotRelatedForeignKey: 'account_id',
|
||||
pivotTable: 'link_accounts_roles',
|
||||
})
|
||||
public users: ManyToMany<typeof User>;
|
||||
|
||||
@manyToMany(() => Permission, {
|
||||
pivotForeignKey: 'role_id',
|
||||
pivotRelatedForeignKey: 'permission_id',
|
||||
pivotTable: 'role_has_permissions',
|
||||
})
|
||||
public permissions: ManyToMany<typeof Permission>;
|
||||
}
|
103
app/Models/User.ts
Normal file
103
app/Models/User.ts
Normal file
|
@ -0,0 +1,103 @@
|
|||
import { DateTime } from 'luxon';
|
||||
import { BaseModel, column, beforeSave, manyToMany, ManyToMany } from '@ioc:Adonis/Lucid/Orm';
|
||||
import Hash from '@ioc:Adonis/Core/Hash';
|
||||
import Role from './Role';
|
||||
import Database from '@ioc:Adonis/Lucid/Database';
|
||||
import Config from '@ioc:Adonis/Core/Config';
|
||||
|
||||
// export default interface IUser {
|
||||
// id: number;
|
||||
// login: string;
|
||||
// email: string;
|
||||
// // password: string;
|
||||
// // createdAt: DateTime;
|
||||
// // updatedAt: DateTime;
|
||||
// // async (user): Promise<void>;
|
||||
// }
|
||||
|
||||
const permissionTable = Config.get('rolePermission.permission_table', 'permissions');
|
||||
const rolePermissionTable = Config.get('rolePermission.role_permission_table', 'role_has_permissions');
|
||||
|
||||
const roleTable = Config.get('rolePermission.role_table', 'roles');
|
||||
const userRoleTable = Config.get('rolePermission.user_role_table', 'link_accounts_roles');
|
||||
|
||||
export default class User extends BaseModel {
|
||||
public static table = 'accounts';
|
||||
|
||||
@column({ isPrimary: true })
|
||||
public id: number;
|
||||
|
||||
@column()
|
||||
public login: string;
|
||||
|
||||
@column()
|
||||
public email: string;
|
||||
|
||||
@column({ serializeAs: null })
|
||||
public password: string;
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime;
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
public updatedAt: DateTime;
|
||||
|
||||
@beforeSave()
|
||||
public static async hashPassword(user) {
|
||||
if (user.$dirty.password) {
|
||||
user.password = await Hash.make(user.password);
|
||||
}
|
||||
}
|
||||
|
||||
@manyToMany(() => Role, {
|
||||
pivotForeignKey: 'account_id',
|
||||
pivotRelatedForeignKey: 'role_id',
|
||||
pivotTable: 'link_accounts_roles',
|
||||
})
|
||||
public roles: ManyToMany<typeof Role>;
|
||||
|
||||
// https://github.com/adonisjs/core/discussions/1872#discussioncomment-132289
|
||||
public async getRoles(this: User): Promise<string[]> {
|
||||
const test = await this.related('roles').query();
|
||||
return test.map((role) => role.name);
|
||||
}
|
||||
|
||||
public async can(permissionNames: Array<string>): Promise<boolean> {
|
||||
// const permissions = await this.getPermissions()
|
||||
// return Acl.check(expression, operand => _.includes(permissions, operand))
|
||||
const hasPermission = await this.checkHasPermissions(this, permissionNames);
|
||||
return hasPermission;
|
||||
}
|
||||
|
||||
private async checkHasPermissions(user: User, permissionNames: Array<string>): Promise<boolean> {
|
||||
let permissionPlaceHolder = '(';
|
||||
let placeholders = new Array(permissionNames.length).fill('?');
|
||||
permissionPlaceHolder += placeholders.join(',');
|
||||
permissionPlaceHolder += ')';
|
||||
|
||||
let {
|
||||
rows: {
|
||||
0: { permissioncount },
|
||||
},
|
||||
} = await Database.rawQuery(
|
||||
'SELECT count("p"."name") as permissionCount FROM ' +
|
||||
roleTable +
|
||||
' r INNER JOIN ' +
|
||||
userRoleTable +
|
||||
' ur ON ur.role_id=r.id AND "ur"."account_id"=? ' +
|
||||
' INNER JOIN ' +
|
||||
rolePermissionTable +
|
||||
' rp ON rp.role_id=r.id ' +
|
||||
' INNER JOIN ' +
|
||||
permissionTable +
|
||||
' p ON rp.permission_id=p.id AND "p"."name" in ' +
|
||||
permissionPlaceHolder +
|
||||
' LIMIT 1',
|
||||
[user.id, ...permissionNames]
|
||||
);
|
||||
|
||||
return permissioncount > 0;
|
||||
}
|
||||
}
|
||||
|
||||
// export default User;
|
74
app/Models/UserRole.ts
Normal file
74
app/Models/UserRole.ts
Normal file
|
@ -0,0 +1,74 @@
|
|||
import {column, BaseModel, belongsTo, BelongsTo, SnakeCaseNamingStrategy} from '@ioc:Adonis/Lucid/Orm'
|
||||
|
||||
import User from 'App/Models/User'
|
||||
import Role from 'App/Models/Role'
|
||||
import { DateTime } from 'luxon'
|
||||
// import moment from 'moment'
|
||||
|
||||
export default class UserRole extends BaseModel {
|
||||
public static namingStrategy = new SnakeCaseNamingStrategy()
|
||||
public static primaryKey = 'id'
|
||||
public static table = 'user_roles'
|
||||
public static selfAssignPrimaryKey = false
|
||||
|
||||
@column({
|
||||
isPrimary: true,
|
||||
})
|
||||
public id: number
|
||||
|
||||
@column({})
|
||||
public user_id: number
|
||||
|
||||
@column({})
|
||||
public role_id: number
|
||||
|
||||
@column({
|
||||
// serialize: (value: DateTime | null) => {
|
||||
// return value ? moment(value).format('lll') : value
|
||||
// },
|
||||
})
|
||||
public created_at: DateTime
|
||||
|
||||
@column({
|
||||
// serialize: (value: DateTime | null) => {
|
||||
// return value ? moment(value).format('lll') : value
|
||||
// },
|
||||
})
|
||||
public updated_at: DateTime
|
||||
|
||||
public static boot() {
|
||||
super.boot()
|
||||
|
||||
this.before('create', async (_modelInstance) => {
|
||||
_modelInstance.created_at = this.formatDateTime(_modelInstance.created_at)
|
||||
_modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at)
|
||||
})
|
||||
this.before('update', async (_modelInstance) => {
|
||||
_modelInstance.created_at = this.formatDateTime(_modelInstance.created_at)
|
||||
_modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at)
|
||||
})
|
||||
}
|
||||
|
||||
private static formatDateTime(datetime) {
|
||||
let value = new Date(datetime)
|
||||
return datetime
|
||||
? value.getFullYear() +
|
||||
'-' +
|
||||
(value.getMonth() + 1) +
|
||||
'-' +
|
||||
value.getDate() +
|
||||
' ' +
|
||||
value.getHours() +
|
||||
':' +
|
||||
value.getMinutes() +
|
||||
':' +
|
||||
value.getSeconds()
|
||||
: datetime
|
||||
}
|
||||
|
||||
@belongsTo(() => User)
|
||||
public user: BelongsTo<typeof User>
|
||||
|
||||
@belongsTo(() => Role)
|
||||
public role: BelongsTo<typeof Role>
|
||||
}
|
22
app/Models/utils.ts
Normal file
22
app/Models/utils.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import Database, {
|
||||
// DatabaseQueryBuilderContract,
|
||||
QueryClientContract,
|
||||
TransactionClientContract,
|
||||
} from "@ioc:Adonis/Lucid/Database";
|
||||
import Config from "@ioc:Adonis/Core/Config";
|
||||
|
||||
export function getUserRoles(
|
||||
userId: number,
|
||||
trx?: TransactionClientContract
|
||||
): Promise<Array<string>> {
|
||||
const { userRole } = Config.get("acl.joinTables");
|
||||
return ((trx || Database) as QueryClientContract | TransactionClientContract)
|
||||
.query()
|
||||
.from("roles")
|
||||
.distinct("roles.slug")
|
||||
.leftJoin(userRole, `${userRole}.role_id`, "roles.id")
|
||||
.where(`${userRole}.user_id`, userId)
|
||||
.then((res) => {
|
||||
return res.map((r) => r.slug);
|
||||
});
|
||||
}
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue