1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
import * as fs from 'fs-extra';
import { logger } from './logger';
logger.info('Starting up server');
logger.info('Creating necessary directories');
fs.ensureDirSync('logs/general');
fs.ensureDirSync('files');
logger.info('Importing server logic');
import { default as app } from './app';
logger.info('Importing database ORM');
import { createRootFolder, rootPathId } from './files';
import { default as seq } from './db/orm';
import { default as ContributorGroup } from './db/model/ContributorGroup';
import { default as File } from './db/model/File';
import { default as User } from './db/model/User';
import { default as UserGroup } from './db/model/UserGroup';
const DEFAULT_PORT: number = 3000;
// Use default port 3000 or port supplied by OS
const port: number = process.env.PORT
? Number(process.env.PORT)
: DEFAULT_PORT;
logger.info(`Port ${port} chosen`);
async function reset(): Promise<void> {
try {
logger.info('Resetting Database');
await seq.sync({
force: true
});
} catch (err) {
logger.error(`Cannot synchronise with database: ${err}`);
process.exit(1);
}
logger.info('Setting up directory structure');
const rootDir: File = new File({
uuid: rootPathId,
mimetype: 'inode/directory',
name: '/'
});
await rootDir.save();
logger.info('Adding admin privileges');
const admin: UserGroup = new UserGroup({
name: 'admin',
canCreateUsers: true,
canDeleteUsers: true,
canEditUsers: true,
canCreateProjects: true,
canDeleteProjects: true,
canEditProjects: true,
isInternal: false,
description: 'Systems admin'
});
const logging: UserGroup = new UserGroup({
name: 'logging',
canAccessLogs: true,
isInternal: false,
description: 'Allows Post to Log'
});
const userManagement: UserGroup = new UserGroup({
name: 'userManagement',
canCreateUsers: true,
canDeleteUsers: true,
canEditUsers: true,
isInternal: false,
description: 'Allows management of other users'
});
const projectAdmin: UserGroup = new UserGroup({
name: 'projectAdmin',
canDeleteProjects: true,
canEditProjects: true,
isInternal: false,
description: 'Admin privileges on other projects '
});
const standardUser: UserGroup = new UserGroup({
name: 'standardUser',
canCreateProjects: true,
isInternal: true,
description: 'Allows creation of new projects'
});
await logging.save();
await admin.save();
await userManagement.save();
await projectAdmin.save();
await standardUser.save();
const projectOwner: ContributorGroup = new ContributorGroup({
name: 'projectOwner',
canCreateFiles: true,
canAddUsers: true,
canRemoveUsers: true,
canDeleteFiles: true,
canDeleteProject: true,
canEditUserPermissions: true,
canViewFiles: true,
isInternal: false,
description: 'Project Admin'
});
const standardProjectUser: ContributorGroup = new ContributorGroup({
name: 'standardProjectUser',
canCreateFiles: true,
canViewFiles: true,
isInternal: false,
description: 'Allows working on a project'
});
const readOnlyProjectUser: ContributorGroup = new ContributorGroup({
name: 'readOnlyProjectUser',
canViewFiles: true,
isInternal: false,
description: 'Allows viewing of files in a project '
});
const projectUserManagement: ContributorGroup = new ContributorGroup({
name: 'projectUserManagement',
canAddUsers: true,
canEditUserPermissions: true,
canRemoveUsers: true,
isInternal: false,
description: 'Allows managament of users in a Project'
});
await projectOwner.save();
await standardProjectUser.save();
await readOnlyProjectUser.save();
await projectUserManagement.save();
logger.info('Adding admin user');
const root: User = new User({
username: 'hafeez',
password: 'pass'
});
await root.save();
createRootFolder();
await root.$set('userGroups', [admin, logging]);
const user: User | null = await User.findOne({
include: [{all: true}],
where: {
username: 'hafeez'
}
});
}
// tslint:disable:no-floating-promises
(async(): Promise<void> => {
try {
logger.info('Authenticating with database');
await seq.authenticate();
} catch (err) {
logger.error(`Cannot authenticate with database: ${err}`);
process.exit(1);
}
//await reset();
logger.info('Booting ExpressJS server');
app.listen(port, (err: Error) => {
if (err) {
return logger.error(`Error on ExpressJS startup: ${err}`);
} else {
return logger.info(`Server started on port ${port}`);
}
});
// tslint:disable-next-line:no-unused-expression
process.send && process.send('ready');
})();
|