mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-13 19:02:45 +00:00
feat: creates admin privileges
This commit is contained in:
@@ -22,6 +22,10 @@ export default class User {
|
||||
return this._model.username;
|
||||
}
|
||||
|
||||
get isAdmin(): boolean {
|
||||
return !!this._model.isAdmin;
|
||||
}
|
||||
|
||||
get accessToken(): string {
|
||||
return this._model.accessTokens.github;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ const UserSchema = new Schema({
|
||||
default: Boolean,
|
||||
},
|
||||
],
|
||||
isAdmin: { type: Boolean, default: false },
|
||||
photo: String,
|
||||
repositories: [String],
|
||||
default: {
|
||||
|
||||
@@ -8,6 +8,7 @@ export interface IUser {
|
||||
github: string;
|
||||
};
|
||||
username: string;
|
||||
isAdmin: boolean;
|
||||
emails: {
|
||||
email: string;
|
||||
default: boolean;
|
||||
|
||||
@@ -3,7 +3,7 @@ import AnonymousError from "../AnonymousError";
|
||||
import Conference from "../Conference";
|
||||
import ConferenceModel from "../database/conference/conferences.model";
|
||||
import { ensureAuthenticated } from "./connection";
|
||||
import { handleError, getUser } from "./route-utils";
|
||||
import { handleError, getUser, isOwnerOrAdmin } from "./route-utils";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -153,11 +153,7 @@ router.post(
|
||||
model = await ConferenceModel.findOne({
|
||||
conferenceID: req.params.conferenceID,
|
||||
});
|
||||
if (model.owners.indexOf(user.model.id) == -1)
|
||||
throw new AnonymousError("not_authorized", {
|
||||
object: req.params.conferenceID,
|
||||
httpStatus: 401,
|
||||
});
|
||||
isOwnerOrAdmin(model.owners, user);
|
||||
}
|
||||
validateConferenceForm(req.body);
|
||||
model.name = req.body.name;
|
||||
@@ -229,11 +225,7 @@ router.get(
|
||||
httpStatus: 404,
|
||||
});
|
||||
const conference = new Conference(data);
|
||||
if (conference.ownerIDs.indexOf(user.model.id) == -1)
|
||||
throw new AnonymousError("not_authorized", {
|
||||
object: req.params.conferenceID,
|
||||
httpStatus: 401,
|
||||
});
|
||||
isOwnerOrAdmin(conference.ownerIDs, user);
|
||||
const o: any = conference.toJSON();
|
||||
o.repositories = (await conference.repositories()).map((r) => r.toJSON());
|
||||
res.json(o);
|
||||
@@ -257,11 +249,7 @@ router.delete(
|
||||
httpStatus: 400,
|
||||
});
|
||||
const conference = new Conference(data);
|
||||
if (conference.ownerIDs.indexOf(user.model.id) == -1)
|
||||
throw new AnonymousError("not_authorized", {
|
||||
object: req.params.conferenceID,
|
||||
httpStatus: 401,
|
||||
});
|
||||
isOwnerOrAdmin(conference.ownerIDs, user);
|
||||
await conference.remove();
|
||||
res.send("ok");
|
||||
} catch (error) {
|
||||
|
||||
@@ -2,7 +2,7 @@ import * as express from "express";
|
||||
import { ensureAuthenticated } from "./connection";
|
||||
|
||||
import * as db from "../database/database";
|
||||
import { getRepo, getUser, handleError } from "./route-utils";
|
||||
import { getRepo, getUser, handleError, isOwnerOrAdmin } from "./route-utils";
|
||||
import { getRepositoryFromGitHub } from "../source/GitHubRepository";
|
||||
import gh = require("parse-github-url");
|
||||
import GitHubBase from "../source/GitHubBase";
|
||||
@@ -81,12 +81,7 @@ router.post(
|
||||
if (repo.status == "preparing" || repo.status == "removing") return;
|
||||
|
||||
const user = await getUser(req);
|
||||
if (repo.owner.id != user.id) {
|
||||
throw new AnonymousError("not_authorized", {
|
||||
object: req.params.repoId,
|
||||
httpStatus: 401,
|
||||
});
|
||||
}
|
||||
isOwnerOrAdmin([repo.owner.id], user);
|
||||
await repo.updateIfNeeded({ force: true });
|
||||
res.json({ status: repo.status });
|
||||
} catch (error) {
|
||||
@@ -109,12 +104,7 @@ router.delete(
|
||||
httpStatus: 410,
|
||||
});
|
||||
const user = await getUser(req);
|
||||
if (repo.owner.id != user.id) {
|
||||
throw new AnonymousError("not_authorized", {
|
||||
object: req.params.repoId,
|
||||
httpStatus: 401,
|
||||
});
|
||||
}
|
||||
isOwnerOrAdmin([repo.owner.id], user);
|
||||
await repo.updateStatus("removing");
|
||||
await removeQueue.add(repo, { jobId: repo.repoId });
|
||||
return res.json({ status: repo.status });
|
||||
@@ -200,12 +190,7 @@ router.get("/:repoId/", async (req: express.Request, res: express.Response) => {
|
||||
if (!repo) return;
|
||||
|
||||
const user = await getUser(req);
|
||||
if (repo.owner.id != user.id) {
|
||||
throw new AnonymousError("not_authorized", {
|
||||
object: req.params.repoId,
|
||||
httpStatus: 401,
|
||||
});
|
||||
}
|
||||
isOwnerOrAdmin([repo.owner.id], user);
|
||||
res.json((await db.getRepository(req.params.repoId)).toJSON());
|
||||
} catch (error) {
|
||||
handleError(error, res);
|
||||
@@ -295,12 +280,7 @@ router.post(
|
||||
if (!repo) return;
|
||||
const user = await getUser(req);
|
||||
|
||||
if (repo.owner.id != user.id) {
|
||||
throw new AnonymousError("not_authorized", {
|
||||
object: req.params.repoId,
|
||||
httpStatus: 401,
|
||||
});
|
||||
}
|
||||
isOwnerOrAdmin([repo.owner.id], user);
|
||||
|
||||
const repoUpdate = req.body;
|
||||
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import * as express from "express";
|
||||
import AnonymizedFile from "../AnonymizedFile";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
import * as db from "../database/database";
|
||||
import UserModel from "../database/users/users.model";
|
||||
import Repository from "../Repository";
|
||||
import GitHubBase from "../source/GitHubBase";
|
||||
import { GitHubRepository } from "../source/GitHubRepository";
|
||||
import User from "../User";
|
||||
import * as io from "@pm2/io";
|
||||
|
||||
@@ -37,6 +33,14 @@ export async function getRepo(
|
||||
}
|
||||
}
|
||||
|
||||
export function isOwnerOrAdmin(authorizedUsers: string[], user: User) {
|
||||
if (authorizedUsers.indexOf(user.model.id) == -1 && !user.isAdmin) {
|
||||
throw new AnonymousError("not_authorized", {
|
||||
httpStatus: 401,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function printError(error: any) {
|
||||
io.notifyError(error, error.value);
|
||||
if (error instanceof AnonymousError) {
|
||||
|
||||
Reference in New Issue
Block a user