Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoBackendAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dojo Project (HES-SO)
Projects
Backend
DojoBackendAPI
Commits
2bc4493b
Commit
2bc4493b
authored
2 years ago
by
michael.minelli
Browse files
Options
Downloads
Patches
Plain Diff
User model class
parent
2694b105
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
ExpressAPI/src/models/User.ts
+91
-0
91 additions, 0 deletions
ExpressAPI/src/models/User.ts
with
91 additions
and
0 deletions
ExpressAPI/src/models/User.ts
0 → 100644
+
91
−
0
View file @
2bc4493b
import
Session
from
'
../controllers/Session
'
;
import
Config
from
'
../config/Config
'
;
import
Toolbox
from
'
../shared/Toolbox
'
;
import
*
as
bcrypt
from
'
bcryptjs
'
;
import
Model
from
'
./Model
'
;
import
db
from
'
../helpers/DatabaseHelper
'
;
class
User
extends
Model
{
userID
:
number
=
null
;
userFirstName
:
string
=
''
;
userLastName
:
string
=
''
;
userMail
:
string
=
null
;
userRole
:
string
=
null
;
userDeleted
:
boolean
=
null
;
userPassword
:
string
=
null
;
unencryptedPassword
:
string
=
null
;
// This value is not set from the db. It's a value that is not null only if we have called createPassword function
currentSession
:
Session
=
null
;
public
async
toJsonObject
(
lightVersion
:
boolean
):
Promise
<
Object
>
{
const
result
=
{
'
id
'
:
this
.
userID
,
'
firstName
'
:
this
.
userFirstName
,
'
lastName
'
:
this
.
userLastName
,
'
mail
'
:
this
.
userMail
,
'
role
'
:
this
.
userRole
,
'
deleted
'
:
this
.
userDeleted
};
return
result
;
};
get
fullName
():
string
{
return
this
.
userLastName
.
toUpperCase
()
+
'
'
+
this
.
userFirstName
;
}
public
importFromJsonObject
(
jsonObject
:
any
)
{
this
.
userID
=
jsonObject
.
id
;
this
.
userFirstName
=
jsonObject
.
firstName
;
this
.
userLastName
=
jsonObject
.
lastName
;
this
.
userMail
=
jsonObject
.
mail
;
this
.
userRole
=
jsonObject
.
role
;
this
.
userDeleted
=
jsonObject
.
deleted
;
}
public
generateHashedPassword
()
{
this
.
userPassword
=
bcrypt
.
hashSync
(
this
.
unencryptedPassword
,
Config
.
userPasswordSaltRounds
);
}
public
replacePassword
(
password
:
string
)
{
this
.
unencryptedPassword
=
password
;
this
.
generateHashedPassword
();
}
public
createPassword
()
{
this
.
unencryptedPassword
=
Toolbox
.
randomString
(
Config
.
userPasswordLength
);
this
.
generateHashedPassword
();
}
public
toDb
():
any
{
return
{
userFirstName
:
Toolbox
.
capitalizeName
(
this
.
userFirstName
),
userLastName
:
Toolbox
.
capitalizeName
(
this
.
userLastName
),
userRole
:
this
.
userRole
,
userMail
:
this
.
userMail
,
userPassword
:
this
.
userPassword
};
}
create
():
Promise
<
void
>
{
return
db
(
User
.
tableName
).
insert
(
this
.
toDb
());
}
update
():
Promise
<
void
>
{
return
db
(
User
.
tableName
).
where
(
'
userID
'
,
this
.
userID
).
update
(
this
.
toDb
());
}
updatePassword
():
Promise
<
void
>
{
return
db
(
User
.
tableName
).
where
(
'
userID
'
,
this
.
userID
).
update
({
'
userPassword
'
:
this
.
userPassword
});
}
del
():
Promise
<
void
>
{
return
db
(
User
.
tableName
).
where
(
'
userID
'
,
this
.
userID
).
update
({
'
userDeleted
'
:
true
});
}
}
export
default
User
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment