feat: use user id instead of username to refer to users

This commit is contained in:
tdurieux
2021-09-06 10:03:10 +02:00
parent 3fbf00c37b
commit e27493dc12
6 changed files with 25 additions and 12 deletions

View File

@@ -146,13 +146,14 @@ async function connect(db) {
console.error(`Repository ${r.fullName} is not found (renamed)`);
}
}
const owner = await UserModel.findOne({ username: r.owner });
await new AnonymizedRepositoryModel({
repoId: r.repoId,
status: r.status,
anonymizeDate: r.anonymizeDate,
lastView: r.lastView,
pageView: r.pageView,
owner: r.owner,
owner: owner?.id,
source: {
accessToken: r.token,
type:

View File

@@ -34,7 +34,7 @@ export default class Repository {
default:
throw new Error("unsupported_source");
}
this.owner = new User(new UserModel({ username: data.owner }));
this.owner = new User(new UserModel({ id: data.owner }));
}
/**

View File

@@ -14,6 +14,10 @@ export default class User {
this._model = model;
}
get id(): string {
return this._model.id;
}
get username(): string {
return this._model.username;
}

View File

@@ -15,7 +15,7 @@ const AnonymizedRepositorySchema = new Schema({
lastView: Date,
pageView: Number,
accessToken: String,
owner: String,
owner: mongoose.Schema.Types.ObjectId,
conference: String,
source: {
type: { type: String },

View File

@@ -12,7 +12,7 @@ export async function connect() {
mongoose.set("useFindAndModify", true);
mongoose.set("useUnifiedTopology", true);
await mongoose.connect(MONGO_URL + "test", {
await mongoose.connect(MONGO_URL + "production", {
authSource: "admin",
useCreateIndex: true,
useFindAndModify: true,

View File

@@ -48,7 +48,7 @@ router.post("/claim", async (req: express.Request, res: express.Response) => {
await AnonymizedRepositoryModel.updateOne(
{ repoId: repoConfig.repoId },
{ $set: { owner: user.username } }
{ $set: { owner: user.model.id } }
);
return res.send("Ok");
} catch (error) {
@@ -65,7 +65,7 @@ router.post(
if (!repo) return;
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" });
}
await repo.anonymize();
@@ -84,7 +84,7 @@ router.delete(
if (!repo) return;
try {
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" });
}
await repo.remove();
@@ -167,7 +167,7 @@ router.get("/:repoId/", async (req: express.Request, res: express.Response) => {
if (!repo) return;
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" });
}
res.json((await db.getRepository(req.params.repoId)).toJSON());
@@ -241,7 +241,7 @@ router.post(
if (!repo) return;
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" });
}
@@ -278,7 +278,11 @@ router.post(
conferenceID: repoUpdate.conference,
});
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");
}
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,
});
if (conf) {
if (new Date() < conf.startDate || new Date() > conf.endDate || conf.status !== "ready") {
await repo.remove()
if (
new Date() < conf.startDate ||
new Date() > conf.endDate ||
conf.status !== "ready"
) {
await repo.remove();
throw new Error("conf_not_activated");
}
conf.repositories.push({