mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-07-07 22:28:01 +02:00
feat: use user id instead of username to refer to users
This commit is contained in:
+2
-1
@@ -146,13 +146,14 @@ async function connect(db) {
|
|||||||
console.error(`Repository ${r.fullName} is not found (renamed)`);
|
console.error(`Repository ${r.fullName} is not found (renamed)`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const owner = await UserModel.findOne({ username: r.owner });
|
||||||
await new AnonymizedRepositoryModel({
|
await new AnonymizedRepositoryModel({
|
||||||
repoId: r.repoId,
|
repoId: r.repoId,
|
||||||
status: r.status,
|
status: r.status,
|
||||||
anonymizeDate: r.anonymizeDate,
|
anonymizeDate: r.anonymizeDate,
|
||||||
lastView: r.lastView,
|
lastView: r.lastView,
|
||||||
pageView: r.pageView,
|
pageView: r.pageView,
|
||||||
owner: r.owner,
|
owner: owner?.id,
|
||||||
source: {
|
source: {
|
||||||
accessToken: r.token,
|
accessToken: r.token,
|
||||||
type:
|
type:
|
||||||
|
|||||||
+1
-1
@@ -34,7 +34,7 @@ export default class Repository {
|
|||||||
default:
|
default:
|
||||||
throw new Error("unsupported_source");
|
throw new Error("unsupported_source");
|
||||||
}
|
}
|
||||||
this.owner = new User(new UserModel({ username: data.owner }));
|
this.owner = new User(new UserModel({ id: data.owner }));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ export default class User {
|
|||||||
this._model = model;
|
this._model = model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get id(): string {
|
||||||
|
return this._model.id;
|
||||||
|
}
|
||||||
|
|
||||||
get username(): string {
|
get username(): string {
|
||||||
return this._model.username;
|
return this._model.username;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const AnonymizedRepositorySchema = new Schema({
|
|||||||
lastView: Date,
|
lastView: Date,
|
||||||
pageView: Number,
|
pageView: Number,
|
||||||
accessToken: String,
|
accessToken: String,
|
||||||
owner: String,
|
owner: mongoose.Schema.Types.ObjectId,
|
||||||
conference: String,
|
conference: String,
|
||||||
source: {
|
source: {
|
||||||
type: { type: String },
|
type: { type: String },
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export async function connect() {
|
|||||||
mongoose.set("useFindAndModify", true);
|
mongoose.set("useFindAndModify", true);
|
||||||
mongoose.set("useUnifiedTopology", true);
|
mongoose.set("useUnifiedTopology", true);
|
||||||
|
|
||||||
await mongoose.connect(MONGO_URL + "test", {
|
await mongoose.connect(MONGO_URL + "production", {
|
||||||
authSource: "admin",
|
authSource: "admin",
|
||||||
useCreateIndex: true,
|
useCreateIndex: true,
|
||||||
useFindAndModify: true,
|
useFindAndModify: true,
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ router.post("/claim", async (req: express.Request, res: express.Response) => {
|
|||||||
|
|
||||||
await AnonymizedRepositoryModel.updateOne(
|
await AnonymizedRepositoryModel.updateOne(
|
||||||
{ repoId: repoConfig.repoId },
|
{ repoId: repoConfig.repoId },
|
||||||
{ $set: { owner: user.username } }
|
{ $set: { owner: user.model.id } }
|
||||||
);
|
);
|
||||||
return res.send("Ok");
|
return res.send("Ok");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -65,7 +65,7 @@ router.post(
|
|||||||
if (!repo) return;
|
if (!repo) return;
|
||||||
|
|
||||||
const user = await getUser(req);
|
const user = await getUser(req);
|
||||||
if (repo.owner.username != user.username) {
|
if (repo.owner.id != user.id) {
|
||||||
return res.status(401).json({ error: "not_authorized" });
|
return res.status(401).json({ error: "not_authorized" });
|
||||||
}
|
}
|
||||||
await repo.anonymize();
|
await repo.anonymize();
|
||||||
@@ -84,7 +84,7 @@ router.delete(
|
|||||||
if (!repo) return;
|
if (!repo) return;
|
||||||
try {
|
try {
|
||||||
const user = await getUser(req);
|
const user = await getUser(req);
|
||||||
if (repo.owner.username != user.username) {
|
if (repo.owner.id != user.id) {
|
||||||
return res.status(401).json({ error: "not_authorized" });
|
return res.status(401).json({ error: "not_authorized" });
|
||||||
}
|
}
|
||||||
await repo.remove();
|
await repo.remove();
|
||||||
@@ -167,7 +167,7 @@ router.get("/:repoId/", async (req: express.Request, res: express.Response) => {
|
|||||||
if (!repo) return;
|
if (!repo) return;
|
||||||
|
|
||||||
const user = await getUser(req);
|
const user = await getUser(req);
|
||||||
if (user.username != repo.model.owner) {
|
if (repo.owner.id != user.id) {
|
||||||
return res.status(401).send({ error: "not_authorized" });
|
return res.status(401).send({ error: "not_authorized" });
|
||||||
}
|
}
|
||||||
res.json((await db.getRepository(req.params.repoId)).toJSON());
|
res.json((await db.getRepository(req.params.repoId)).toJSON());
|
||||||
@@ -241,7 +241,7 @@ router.post(
|
|||||||
if (!repo) return;
|
if (!repo) return;
|
||||||
const user = await getUser(req);
|
const user = await getUser(req);
|
||||||
|
|
||||||
if (repo.owner.username != user.username) {
|
if (repo.owner.id != user.id) {
|
||||||
return res.status(401).json({ error: "not_authorized" });
|
return res.status(401).json({ error: "not_authorized" });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,7 +278,11 @@ router.post(
|
|||||||
conferenceID: repoUpdate.conference,
|
conferenceID: repoUpdate.conference,
|
||||||
});
|
});
|
||||||
if (conf) {
|
if (conf) {
|
||||||
if (new Date() < conf.startDate || new Date() > conf.endDate || conf.status !== "ready") {
|
if (
|
||||||
|
new Date() < conf.startDate ||
|
||||||
|
new Date() > conf.endDate ||
|
||||||
|
conf.status !== "ready"
|
||||||
|
) {
|
||||||
throw new Error("conf_not_activated");
|
throw new Error("conf_not_activated");
|
||||||
}
|
}
|
||||||
const f = conf.repositories.filter((r) => r.id == repo.model.id);
|
const f = conf.repositories.filter((r) => r.id == repo.model.id);
|
||||||
@@ -348,8 +352,12 @@ router.post("/", async (req: express.Request, res: express.Response) => {
|
|||||||
conferenceID: repoUpdate.conference,
|
conferenceID: repoUpdate.conference,
|
||||||
});
|
});
|
||||||
if (conf) {
|
if (conf) {
|
||||||
if (new Date() < conf.startDate || new Date() > conf.endDate || conf.status !== "ready") {
|
if (
|
||||||
await repo.remove()
|
new Date() < conf.startDate ||
|
||||||
|
new Date() > conf.endDate ||
|
||||||
|
conf.status !== "ready"
|
||||||
|
) {
|
||||||
|
await repo.remove();
|
||||||
throw new Error("conf_not_activated");
|
throw new Error("conf_not_activated");
|
||||||
}
|
}
|
||||||
conf.repositories.push({
|
conf.repositories.push({
|
||||||
|
|||||||
Reference in New Issue
Block a user