Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
foyzulkarim committed Mar 31, 2022
1 parent e71403c commit 6f88edf
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 148 deletions.
6 changes: 6 additions & 0 deletions server/setup/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ require("dotenv").config();
const logger = require("../src/core/logger");

const { migrate: userMigrate } = require("./users");
const { migrate: resourceMigrate } = require("./resources");
const { migrate: roleMigrate } = require("./roles");
const { migrate: permissionMigrate } = require("./permissions");

logger.info("Migration starting");
const isMongoDbUrl = JSON.parse(
Expand All @@ -18,6 +21,9 @@ const migrate = async () => {
await mongoose.connect(uri, options);
logger.info("Connected to MongoDB");
await userMigrate(logger);
await resourceMigrate(logger);
await roleMigrate(logger);
await permissionMigrate(logger);
logger.info(`Migration finished`);
process.exit(0);
};
Expand Down
27 changes: 25 additions & 2 deletions server/setup/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const parser = require("jsonc-parser");

const dataStr = fs.readFileSync("./setup/permissions.jsonc", "utf8");

const { save, searchOne, update } = require("../src/core/repository");
const {
save,
searchOne,
update,
updateAll,
} = require("../src/core/repository");
const { name: permissionModel } = require("../src/modules/permission/model");
const { name: resourceModel } = require("../src/modules/resource/model");

Expand Down Expand Up @@ -51,4 +56,22 @@ const seed = async (logger) => {
logger.info(`Seeding users finished`);
};

module.exports = { seed };
const migrate = async (logger) => {
logger.info(`Starting migration of permissions`);
const superadminUser = await searchOne({ username: "superadmin" }, "User");
if (!superadminUser) {
throw new Error("Superadmin user not found");
}

await updateAll(
{},
{
createdBy: superadminUser._id,
updatedBy: superadminUser._id,
},
permissionModel
);
logger.info(`Migration of permissions finished`);
};

module.exports = { seed, migrate };
176 changes: 175 additions & 1 deletion server/setup/permissions.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,120 @@

// Admin users api permissions

// admin roles api permissions

// /api/roles/detail for Admin
{
"resourceName": "/api/roles/detail",
"resourceAlias": "Roles detail API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/roles/create for Admin
{
"resourceName": "/api/roles/create",
"resourceAlias": "Roles create API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/roles/update for Admin
{
"resourceName": "/api/roles/update",
"resourceAlias": "Roles update API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/roles/search for Admin
{
"resourceName": "/api/roles/search",
"resourceAlias": "Roles search API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/roles/count for Admin
{
"resourceName": "/api/roles/count",
"resourceAlias": "Roles count API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/roles/delete for Admin
{
"resourceName": "/api/roles/delete",
"resourceAlias": "Roles delete API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},

// admin permissions api permissions

// /api/permissions/detail for Admin
{
"resourceName": "/api/permissions/detail",
"resourceAlias": "Permissions detail API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/permissions/create for Admin
{
"resourceName": "/api/permissions/create",
"resourceAlias": "Permissions create API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/permissions/update for admin
{
"resourceName": "/api/permissions/update",
"resourceAlias": "Permissions update API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/permissions/search for admin
{
"resourceName": "/api/permissions/search",
"resourceAlias": "Permissions search API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/permissions/count for admin
{
"resourceName": "/api/permissions/count",
"resourceAlias": "Permissions count API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /api/permissions/delete for admin
{
"resourceName": "/api/permissions/delete",
"resourceAlias": "Permissions delete API",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},

// /api/users/detail for admin
{
"resourceName": "/api/users/detail",
Expand Down Expand Up @@ -301,7 +415,7 @@
"isAllowed": true,
"isDisabled": false
},
// /api/roles/search for superadmin
// /api/roles/search for admin
{
"resourceName": "/api/roles/search",
"resourceAlias": "Roles search API",
Expand Down Expand Up @@ -460,6 +574,36 @@
"isDisabled": false
},

// admin permissions for roles pages

// /roles permissions for admin
{
"resourceName": "/roles",
"resourceAlias": "Roles menu",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /roles/list permissions for admin
{
"resourceName": "/roles/list",
"resourceAlias": "Roles list",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /roles/new permission for admin
{
"resourceName": "/roles/new",
"resourceAlias": "Roles new",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},

// superadmin permissions for resources pages

// /resources permissions for superadmin
Expand Down Expand Up @@ -520,6 +664,36 @@
"isDisabled": false
},

// admin permissions for permissions pages

// /permissions permissions for admin
{
"resourceName": "/permissions",
"resourceAlias": "Permissions menu",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /permissions/list permissions for admin
{
"resourceName": "/permissions/list",
"resourceAlias": "Permissions list",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},
// /permissions/new permission for admin
{
"resourceName": "/permissions/new",
"resourceAlias": "Permissions new",
"roleName": "admin",
"roleAlias": "Admin",
"isAllowed": true,
"isDisabled": false
},

// user permissions for products pages

// /products permissions for user
Expand Down
34 changes: 26 additions & 8 deletions server/setup/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,43 @@ const fs = require("fs");
const parser = require("jsonc-parser");

const dataStr = fs.readFileSync("./setup/resources.jsonc", "utf8");
const { save, searchOne } = require("../src/core/repository");
const { name: model } = require("../src/modules/resource/model");
const { save, searchOne, updateAll } = require("../src/core/repository");
const { name: modelName } = require("../src/modules/resource/model");

// const model = "Resource";
const seed = async (logger) => {
const data = parser.parse(dataStr);
await Promise.all(
data.map(async (item) => {
logger.info(`Checking if ${model} ${item.name} exists`);
const itemExists = await searchOne({ name: item.name }, model);
logger.info(`Checking if ${modelName} ${item.name} exists`);
const itemExists = await searchOne({ name: item.name }, modelName);
if (!itemExists) {
const savedItem = await save(item, model);
const savedItem = await save(item, modelName);
logger.info(`Saved role id: ${savedItem._id}`);
} else {
logger.info(`${model} ${item.name} already exists`);
logger.info(`${modelName} ${item.name} already exists`);
}
})
);
logger.info(`${model} seeding finished`);
logger.info(`${modelName} seeding finished`);
};

module.exports = { seed };
const migrate = async (logger) => {
logger.info(`${modelName} starting`);
const superadminUser = await searchOne({ username: "superadmin" }, "User");
if (!superadminUser) {
throw new Error("Superadmin user not found");
}

await updateAll(
{},
{
createdBy: superadminUser._id,
updatedBy: superadminUser._id,
},
modelName
);
logger.info(`${modelName} seeding finished`);
};

module.exports = { seed, migrate };
22 changes: 20 additions & 2 deletions server/setup/roles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const data = require("./roles.json");
const { save, searchOne } = require("../src/core/repository");
const { save, searchOne, updateAll } = require("../src/core/repository");
const { modelName } = require("../src/modules/role/service");

const seed = async (logger) => {
Expand All @@ -18,4 +18,22 @@ const seed = async (logger) => {
logger.info(`Seeding ${modelName} finished`);
};

module.exports = { seed };
const migrate = async (logger) => {
logger.info(`Starting migration of ${modelName}`);
const superadminUser = await searchOne({ name: "superadmin" }, modelName);
if (!superadminUser) {
throw new Error(`Superadmin user not found`);
}

await updateAll(
{},
{
createdBy: superadminUser._id,
updatedBy: superadminUser._id,
},
modelName
);
logger.info(`Migration of ${modelName} finished`);
};

module.exports = { seed, migrate };
15 changes: 14 additions & 1 deletion server/src/modules/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ const {
authorizeRequest,
} = require("../../common/middlewares");

const { name: ModelName } = require("./model");

const processRequest = async (req, res, next) => {
req.modelName = ModelName;
return next();
};

const init = async (app) => {
app.use("/api/auth", authRoutes);
app.use("/api/users", authenticateRequest, authorizeRequest, userRoutes);
app.use(
"/api/users",
authenticateRequest,
authorizeRequest,
processRequest,
userRoutes
);
return app;
};

Expand Down
5 changes: 3 additions & 2 deletions server/src/modules/auth/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ userSchema.post("save", (error, doc, next) => {
}
});

const User = mongoose.model("User", userSchema);
const ModelName = "User";
const User = mongoose.model(ModelName, userSchema);

async function getPasswordHash(password) {
const hash = await bcrypt.hash(password, 10);
Expand All @@ -75,4 +76,4 @@ User.setPassword = async (model, newPassword) => {
return { passwordHash, ...model };
};

module.exports = User;
module.exports = { Model: User, name: ModelName };
Loading

0 comments on commit 6f88edf

Please sign in to comment.