diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-27 16:55:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-27 16:55:03 +0200 |
commit | 954605a804da399317ca62afa2fb9244afa11ebf (patch) | |
tree | de6ee69280bfb928bc01c29430e13d5b820e921a /server/models | |
parent | e02573ad67626210ed279bad321ee139094921a1 (diff) | |
download | PeerTube-954605a804da399317ca62afa2fb9244afa11ebf.tar.gz PeerTube-954605a804da399317ca62afa2fb9244afa11ebf.tar.zst PeerTube-954605a804da399317ca62afa2fb9244afa11ebf.zip |
Support roles with rights and add moderator role
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/user/user-interface.ts | 9 | ||||
-rw-r--r-- | server/models/user/user.ts | 29 |
2 files changed, 23 insertions, 15 deletions
diff --git a/server/models/user/user-interface.ts b/server/models/user/user-interface.ts index 1b5233eaf..49c75aa3b 100644 --- a/server/models/user/user-interface.ts +++ b/server/models/user/user-interface.ts | |||
@@ -3,15 +3,16 @@ import * as Promise from 'bluebird' | |||
3 | 3 | ||
4 | // Don't use barrel, import just what we need | 4 | // Don't use barrel, import just what we need |
5 | import { User as FormattedUser } from '../../../shared/models/users/user.model' | 5 | import { User as FormattedUser } from '../../../shared/models/users/user.model' |
6 | import { UserRole } from '../../../shared/models/users/user-role.type' | ||
7 | import { ResultList } from '../../../shared/models/result-list.model' | 6 | import { ResultList } from '../../../shared/models/result-list.model' |
8 | import { AuthorInstance } from '../video/author-interface' | 7 | import { AuthorInstance } from '../video/author-interface' |
8 | import { UserRight } from '../../../shared/models/users/user-right.enum' | ||
9 | import { UserRole } from '../../../shared/models/users/user-role' | ||
9 | 10 | ||
10 | export namespace UserMethods { | 11 | export namespace UserMethods { |
12 | export type HasRight = (this: UserInstance, right: UserRight) => boolean | ||
11 | export type IsPasswordMatch = (this: UserInstance, password: string) => Promise<boolean> | 13 | export type IsPasswordMatch = (this: UserInstance, password: string) => Promise<boolean> |
12 | 14 | ||
13 | export type ToFormattedJSON = (this: UserInstance) => FormattedUser | 15 | export type ToFormattedJSON = (this: UserInstance) => FormattedUser |
14 | export type IsAdmin = (this: UserInstance) => boolean | ||
15 | export type IsAbleToUploadVideo = (this: UserInstance, videoFile: Express.Multer.File) => Promise<boolean> | 16 | export type IsAbleToUploadVideo = (this: UserInstance, videoFile: Express.Multer.File) => Promise<boolean> |
16 | 17 | ||
17 | export type CountTotal = () => Promise<number> | 18 | export type CountTotal = () => Promise<number> |
@@ -31,7 +32,7 @@ export namespace UserMethods { | |||
31 | export interface UserClass { | 32 | export interface UserClass { |
32 | isPasswordMatch: UserMethods.IsPasswordMatch, | 33 | isPasswordMatch: UserMethods.IsPasswordMatch, |
33 | toFormattedJSON: UserMethods.ToFormattedJSON, | 34 | toFormattedJSON: UserMethods.ToFormattedJSON, |
34 | isAdmin: UserMethods.IsAdmin, | 35 | hasRight: UserMethods.HasRight, |
35 | isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo, | 36 | isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo, |
36 | 37 | ||
37 | countTotal: UserMethods.CountTotal, | 38 | countTotal: UserMethods.CountTotal, |
@@ -62,7 +63,7 @@ export interface UserInstance extends UserClass, UserAttributes, Sequelize.Insta | |||
62 | 63 | ||
63 | isPasswordMatch: UserMethods.IsPasswordMatch | 64 | isPasswordMatch: UserMethods.IsPasswordMatch |
64 | toFormattedJSON: UserMethods.ToFormattedJSON | 65 | toFormattedJSON: UserMethods.ToFormattedJSON |
65 | isAdmin: UserMethods.IsAdmin | 66 | hasRight: UserMethods.HasRight |
66 | } | 67 | } |
67 | 68 | ||
68 | export interface UserModel extends UserClass, Sequelize.Model<UserInstance, UserAttributes> {} | 69 | export interface UserModel extends UserClass, Sequelize.Model<UserInstance, UserAttributes> {} |
diff --git a/server/models/user/user.ts b/server/models/user/user.ts index 074c9c121..3c625e450 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts | |||
@@ -1,17 +1,17 @@ | |||
1 | import { values } from 'lodash' | ||
2 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
3 | import * as Promise from 'bluebird' | 2 | import * as Promise from 'bluebird' |
4 | 3 | ||
5 | import { getSort } from '../utils' | 4 | import { getSort } from '../utils' |
6 | import { USER_ROLES } from '../../initializers' | ||
7 | import { | 5 | import { |
8 | cryptPassword, | 6 | cryptPassword, |
9 | comparePassword, | 7 | comparePassword, |
10 | isUserPasswordValid, | 8 | isUserPasswordValid, |
11 | isUserUsernameValid, | 9 | isUserUsernameValid, |
12 | isUserDisplayNSFWValid, | 10 | isUserDisplayNSFWValid, |
13 | isUserVideoQuotaValid | 11 | isUserVideoQuotaValid, |
12 | isUserRoleValid | ||
14 | } from '../../helpers' | 13 | } from '../../helpers' |
14 | import { UserRight, USER_ROLE_LABELS, hasUserRight } from '../../../shared' | ||
15 | 15 | ||
16 | import { addMethodsToModel } from '../utils' | 16 | import { addMethodsToModel } from '../utils' |
17 | import { | 17 | import { |
@@ -23,8 +23,8 @@ import { | |||
23 | 23 | ||
24 | let User: Sequelize.Model<UserInstance, UserAttributes> | 24 | let User: Sequelize.Model<UserInstance, UserAttributes> |
25 | let isPasswordMatch: UserMethods.IsPasswordMatch | 25 | let isPasswordMatch: UserMethods.IsPasswordMatch |
26 | let hasRight: UserMethods.HasRight | ||
26 | let toFormattedJSON: UserMethods.ToFormattedJSON | 27 | let toFormattedJSON: UserMethods.ToFormattedJSON |
27 | let isAdmin: UserMethods.IsAdmin | ||
28 | let countTotal: UserMethods.CountTotal | 28 | let countTotal: UserMethods.CountTotal |
29 | let getByUsername: UserMethods.GetByUsername | 29 | let getByUsername: UserMethods.GetByUsername |
30 | let listForApi: UserMethods.ListForApi | 30 | let listForApi: UserMethods.ListForApi |
@@ -76,8 +76,14 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
76 | } | 76 | } |
77 | }, | 77 | }, |
78 | role: { | 78 | role: { |
79 | type: DataTypes.ENUM(values(USER_ROLES)), | 79 | type: DataTypes.INTEGER, |
80 | allowNull: false | 80 | allowNull: false, |
81 | validate: { | ||
82 | roleValid: value => { | ||
83 | const res = isUserRoleValid(value) | ||
84 | if (res === false) throw new Error('Role is not valid.') | ||
85 | } | ||
86 | } | ||
81 | }, | 87 | }, |
82 | videoQuota: { | 88 | videoQuota: { |
83 | type: DataTypes.BIGINT, | 89 | type: DataTypes.BIGINT, |
@@ -120,9 +126,9 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
120 | loadByUsernameOrEmail | 126 | loadByUsernameOrEmail |
121 | ] | 127 | ] |
122 | const instanceMethods = [ | 128 | const instanceMethods = [ |
129 | hasRight, | ||
123 | isPasswordMatch, | 130 | isPasswordMatch, |
124 | toFormattedJSON, | 131 | toFormattedJSON, |
125 | isAdmin, | ||
126 | isAbleToUploadVideo | 132 | isAbleToUploadVideo |
127 | ] | 133 | ] |
128 | addMethodsToModel(User, classMethods, instanceMethods) | 134 | addMethodsToModel(User, classMethods, instanceMethods) |
@@ -139,6 +145,10 @@ function beforeCreateOrUpdate (user: UserInstance) { | |||
139 | 145 | ||
140 | // ------------------------------ METHODS ------------------------------ | 146 | // ------------------------------ METHODS ------------------------------ |
141 | 147 | ||
148 | hasRight = function (this: UserInstance, right: UserRight) { | ||
149 | return hasUserRight(this.role, right) | ||
150 | } | ||
151 | |||
142 | isPasswordMatch = function (this: UserInstance, password: string) { | 152 | isPasswordMatch = function (this: UserInstance, password: string) { |
143 | return comparePassword(password, this.password) | 153 | return comparePassword(password, this.password) |
144 | } | 154 | } |
@@ -150,6 +160,7 @@ toFormattedJSON = function (this: UserInstance) { | |||
150 | email: this.email, | 160 | email: this.email, |
151 | displayNSFW: this.displayNSFW, | 161 | displayNSFW: this.displayNSFW, |
152 | role: this.role, | 162 | role: this.role, |
163 | roleLabel: USER_ROLE_LABELS[this.role], | ||
153 | videoQuota: this.videoQuota, | 164 | videoQuota: this.videoQuota, |
154 | createdAt: this.createdAt, | 165 | createdAt: this.createdAt, |
155 | author: { | 166 | author: { |
@@ -174,10 +185,6 @@ toFormattedJSON = function (this: UserInstance) { | |||
174 | return json | 185 | return json |
175 | } | 186 | } |
176 | 187 | ||
177 | isAdmin = function (this: UserInstance) { | ||
178 | return this.role === USER_ROLES.ADMIN | ||
179 | } | ||
180 | |||
181 | isAbleToUploadVideo = function (this: UserInstance, videoFile: Express.Multer.File) { | 188 | isAbleToUploadVideo = function (this: UserInstance, videoFile: Express.Multer.File) { |
182 | if (this.videoQuota === -1) return Promise.resolve(true) | 189 | if (this.videoQuota === -1) return Promise.resolve(true) |
183 | 190 | ||