/[sudobot]/trunk/src/api/controllers/UserController.ts
ViewVC logotype

Diff of /trunk/src/api/controllers/UserController.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 349 by rakin, Mon Jul 29 17:29:43 2024 UTC revision 396 by rakin, Mon Jul 29 17:30:01 2024 UTC
# Line 1  Line 1 
1  import { Request } from "express";  /**
2    * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
5    *
6    * SudoBot is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Affero General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10    *
11    * SudoBot is distributed in the hope that it will be useful, but
12    * WITHOUT ANY WARRANTY; without even the implied warranty of
13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    * GNU Affero General Public License for more details.
15    *
16    * You should have received a copy of the GNU Affero General Public License
17    * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18    */
19    
20    import Request from "../Request";
21  import User from "../../models/User";  import User from "../../models/User";
22  import Controller from "../Controller";  import Controller from "../Controller";
23  import { body } from 'express-validator';  import { body } from 'express-validator';
24  import bcrypt from 'bcrypt';  import bcrypt from 'bcrypt';
25  import jwt from 'jsonwebtoken';  import jwt from 'jsonwebtoken';
26  import KeyValuePair from "../../types/KeyValuePair";  import KeyValuePair from "../../types/KeyValuePair";
27  import Response from "../Response";  import { NextFunction, Response as ExpressResponse } from "express";
28  import ValidatorError from "../middleware/ValidatorError";  import ValidatorError from "../middleware/ValidatorError";
29  import RequireAuth from "../middleware/RequireAuth";  import RequireAuth from "../middleware/RequireAuth";
30    
31    function RequireAdmin(request: Request, response: ExpressResponse, next: NextFunction) {
32        if (!request.user?.isAdmin) {
33            response.status(403).send({ error: "Forbidden", code: 403 });
34            return;
35        }
36    
37        next();
38    }
39    
40  export default class UserController extends Controller {  export default class UserController extends Controller {
41      middleware(): KeyValuePair<Function[]> {      middleware(): KeyValuePair<Function[]> {
42          return {          return {
43                index: [RequireAuth, RequireAdmin],
44              create: [              create: [
45                    RequireAuth,
46                    RequireAdmin,
47                  body(["password"]).isLength({ min: 2 }),                  body(["password"]).isLength({ min: 2 }),
48                  body(["username"]).custom(async username => {                  body(["username"]).custom(async username => {
49                      const user = await User.findOne({ username });                      const user = await User.findOne({ username });
# Line 40  export default class UserController exte Line 71  export default class UserController exte
71      }      }
72    
73      public async index() {      public async index() {
         return new Response(403);  
74          return await User.find().select(["_id", "username", "createdAt"]).limit(30);          return await User.find().select(["_id", "username", "createdAt"]).limit(30);
75      }      }
76    
77      public async create(request: Request) {      public async create(request: Request) {
         return new Response(403);  
   
78          const user = new User();          const user = new User();
79    
80          user.username = request.body.username;          user.username = request.body.username;
# Line 155  export default class UserController exte Line 183  export default class UserController exte
183              message: "Login successful",              message: "Login successful",
184              username,              username,
185              token,              token,
186              expires: new Date(user.tokenUpdatedAt!.getTime() + (2 * 24 * 60 * 60 * 1000))              expires: new Date(user.tokenUpdatedAt!.getTime() + (2 * 24 * 60 * 60 * 1000)),
187                guilds: this.client.guilds.cache.filter(g => user.guilds.includes(g.id) ?? false)
188          };          };
189      }      }
190  }  }

Legend:
Removed from v.349  
changed lines
  Added in v.396

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26