Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotrajano committed Jul 11, 2020
1 parent 575c765 commit eb48bef
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 40 deletions.
34 changes: 30 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const feature = require(`./routes/feature`);
const admin = require(`./routes/admin`);
const bug = require(`./routes/bug`);
const api = require(`./routes/api`);
const login = require(`./routes/login`);

app.use(
session({ secret: "trackpath", resave: false, saveUninitialized: true })
Expand All @@ -42,6 +41,9 @@ app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
res.locals.user = req.user || null;

console.log(req.user);

next();
});
Expand All @@ -62,10 +64,34 @@ app.use(`/feature`, feature);
app.use(`/admin`, admin);
app.use(`/bug`, bug);
app.use(`/api`, api);
app.use(`/login`, login);

app.get("/", (req, res) => {
res.redirect("/login");
app.get(`/`, (req, res) => {
if (req.isAuthenticated()) {
res.render(`feature/feature`);
} else {
res.render(`login/login`);
}
});

app.get(`/login`, (req, res) => {
if (req.isAuthenticated()) {
res.render(`feature/feature`);
} else {
res.render(`login/login`);
}
});

app.post(`/login`, (req, res, next) => {
passport.authenticate("local", {
successRedirect: "/feature",
failureRedirect: "/login",
failureFlash: true,
})(req, res, next);
});

app.get(`/logout`, (req, res) => {
req.logOut();
res.redirect(`/login`);
});

app.listen(SERVER_PORT, () => {
Expand Down
17 changes: 17 additions & 0 deletions helpers/permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
isAuthenticated: (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
req.flash("error_msg", "User not authenticated!");
res.redirect("/login");
},
isAdmin: (req, res, next) => {
if (req.isAuthenticated() && req.user.isAdmin === 1) {
return next();
}
req.flash("error_msg", "User not authorized!");
//res.redirect("/notauthorized");
res.redirect("/notauthorized");
},
};
41 changes: 41 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,44 @@ span.psw {
width: 100%;
}
}

.dropbtn {
font-size: 20px;
margin-top: 10px;
margin-right: 15px;
cursor: pointer;
}

.dropbtn:hover,
.dropbtn:focus {
}

.dropdown {
position: relative;
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

.dropdown a:hover {
background-color: #ddd;
}

.show {
display: block;
}
6 changes: 3 additions & 3 deletions public/js/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ function w3_open() {
document.getElementById("mySidebar").style.width = "23%";
document.getElementById("mySidebar").style.display = "block";
document.getElementById("openNav").style.display = "none";
localStorage.setItem("openSidebar", true);
sessionStorage.setItem("openSidebar", true);
}

function w3_close() {
document.getElementById("main").style.marginLeft = "0%";
document.getElementById("mySidebar").style.display = "none";
document.getElementById("openNav").style.display = "inline-block";
localStorage.setItem("openSidebar", false);
sessionStorage.setItem("openSidebar", false);
}

if (localStorage.getItem("openSidebar") === "true") {
if (sessionStorage.getItem("openSidebar") === "true") {
w3_open();
} else {
w3_close();
Expand Down
13 changes: 0 additions & 13 deletions routes/login.js → routes/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const passport = require(`passport`);

router.post(`/addUser`, (req, res) => {
let user = req.body;
user.isAdmin = 0;

bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(user.password, salt, (err, hash) => {
Expand All @@ -24,16 +23,4 @@ router.post(`/addUser`, (req, res) => {
res.render(`login/login`);
});

router.get(`/`, (req, res) => {
res.render(`login/login`);
});

router.post(`/`, (req, res, next) => {
passport.authenticate("local", {
successRedirect: "/project",
failureRedirect: "/login",
failureFlash: true,
})(req, res, next);
});

module.exports = router;
3 changes: 2 additions & 1 deletion routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const Project = require(`../models/Project`);
const Feature = require(`../models/Feature`);
const FeatureStatus = require(`../models/FeatureStatus`);
const Bug = require(`../models/Bug`);
const { isAuthenticated, isAdmin } = require(`../helpers/permissions`);

router.get(`/`, async (req, res) => {
router.get(`/`, isAdmin, async (req, res) => {
let totalProjects = 0;
let totalFeatures = 0;
let totalIssuesSolved = 0;
Expand Down
11 changes: 6 additions & 5 deletions routes/bug.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const router = express.Router();
const Bug = require(`../models/Bug`);
const Project = require(`../models/Project`);
const moment = require("moment");
const { isAuthenticated, isAdmin } = require(`../helpers/permissions`);

router.get(`/`, (req, res) => {
router.get(`/`, isAuthenticated, (req, res) => {
res.render(`bug/bugs`);
});

router.get(`/add/:projectID`, (req, res) => {
router.get(`/add/:projectID`, isAuthenticated, (req, res) => {
Project.findByPK(req.params.projectID)
.then((bug) => {
res.render(`bug/addBug`, { project: bug });
Expand All @@ -19,7 +20,7 @@ router.get(`/add/:projectID`, (req, res) => {
});
});

router.post(`/add`, (req, res) => {
router.post(`/add`, isAuthenticated, (req, res) => {
const bug = req.body;
bug.CreatedAt = moment().format("YYYY-MM-DD");

Expand All @@ -34,7 +35,7 @@ router.post(`/add`, (req, res) => {
});
});

router.get(`/solveIssue/:bugID`, (req, res) => {
router.get(`/solveIssue/:bugID`, isAuthenticated, (req, res) => {
Bug.findByPK(req.params.bugID)
.then((bug) => {
Project.findByPK(bug.ProjectID)
Expand All @@ -53,7 +54,7 @@ router.get(`/solveIssue/:bugID`, (req, res) => {
});
});

router.post(`/solveIssue`, (req, res) => {
router.post(`/solveIssue`, isAuthenticated, (req, res) => {
const bug = req.body;

Bug.updateSolveIssue(bug)
Expand Down
13 changes: 7 additions & 6 deletions routes/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const Project = require(`../models/Project`);
const Feature = require(`../models/Feature`);
const StatusProgress = require(`../models/StatusProgress`);
const moment = require("moment");
const { isAuthenticated, isAdmin } = require(`../helpers/permissions`);

router.get(`/`, (req, res) => {
router.get(`/`, isAuthenticated, (req, res) => {
res.render("feature/feature");
});

router.get(`/getFeaturesByProject/:id`, (req, res) => {
router.get(`/getFeaturesByProject/:id`, isAuthenticated, (req, res) => {
Feature.findFeaturesByProjects(req.params.id)
.then((features) => {
res.send(features);
Expand All @@ -20,7 +21,7 @@ router.get(`/getFeaturesByProject/:id`, (req, res) => {
});
});

router.get(`/new/:id`, (req, res) => {
router.get(`/new/:id`, isAuthenticated, (req, res) => {
Project.findByPK(req.params.id)
.then((project) => {
res.render("feature/newFeature", { project: project });
Expand All @@ -31,7 +32,7 @@ router.get(`/new/:id`, (req, res) => {
});
});

router.post(`/new`, (req, res) => {
router.post(`/new`, isAuthenticated, (req, res) => {
const feature = {
ID: req.body.id,
TitleFeature: req.body.titleFeature,
Expand All @@ -55,7 +56,7 @@ router.post(`/new`, (req, res) => {
});
});

router.get(`/solveIssue/:id`, (req, res) => {
router.get(`/solveIssue/:id`, isAuthenticated, (req, res) => {
Feature.findByPK(req.params.id)
.then((feature) => {
Project.findByPK(feature.ProjectID)
Expand All @@ -74,7 +75,7 @@ router.get(`/solveIssue/:id`, (req, res) => {
});
});

router.post(`/solveIssue`, (req, res) => {
router.post(`/solveIssue`, isAuthenticated, (req, res) => {
const feature = {
ID: req.body.featureID,
DescriptionFeature: req.body.description,
Expand Down
11 changes: 6 additions & 5 deletions routes/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const express = require(`express`);
const router = express.Router();
const Project = require(`../models/Project`);
const moment = require(`moment`);
const { isAdmin } = require(`../helpers/permissions`);

router.get(`/`, (req, res) => {
router.get(`/`, isAdmin, (req, res) => {
Project.findAll()
.then((projects) => {
res.render("project/project", { Projects: projects });
Expand All @@ -13,11 +14,11 @@ router.get(`/`, (req, res) => {
});
});

router.get(`/new`, (req, res) => {
router.get(`/new`, isAdmin, (req, res) => {
res.render("project/newProject");
});

router.post(`/new`, (req, res) => {
router.post(`/new`, isAdmin, (req, res) => {
let errors = [];
let project = req.body;

Expand Down Expand Up @@ -58,7 +59,7 @@ router.post(`/new`, (req, res) => {
}
});

router.get(`/edit/:id`, (req, res) => {
router.get(`/edit/:id`, isAdmin, (req, res) => {
Project.findByPK(req.params.id)
.then((project) => {
res.render("project/newProject", {
Expand All @@ -76,7 +77,7 @@ router.get(`/edit/:id`, (req, res) => {
});
});

router.post(`/delete`, (req, res) => {
router.post(`/delete`, isAdmin, (req, res) => {
Project.remove(req.body.id)
.then(() => {
req.flash("error_msg", "Project has been deleted!");
Expand Down
10 changes: 10 additions & 0 deletions sql_db_scripts/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ CREATE TABLE `featurestatus` (
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`isAdmin` tinyint(1) NOT NULL,
`password` varchar(400) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;




insert into StatusProgress (StatusProgressValue,StatusProgress) values (0, "Not Started");
Expand Down
5 changes: 4 additions & 1 deletion views/layouts/main.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
<link rel="stylesheet" href="/css/style.css">

<body>

{{#if user}}
<div class="w3-sidebar w3-bar-block w3-card" style="display:none" id="mySidebar">
<button class="w3-bar-item w3-button w3-large" onclick="w3_close()">Close &times;</button>
{{#if user.isAdmin}}
<a href="/admin" class="w3-bar-item w3-button"><i class="fa fa-tachometer" aria-hidden="true"></i> Dashboard</a>
<a href="/project" class="w3-bar-item w3-button"><i class="fa fa-terminal" aria-hidden="true"></i> Projects</a>
{{/if}}
<a href="/feature" class="w3-bar-item w3-button"><i class="fa fa-plus-square" aria-hidden="true"></i> New
Features</a>
<a href="/bug" class="w3-bar-item w3-button"><i class="fa fa-bug" aria-hidden="true"></i> Bugs</a>
Expand All @@ -25,6 +27,7 @@
<a href="#" class="w3-bar-item w3-button"><i class="fa fa-rss" aria-hidden="true"></i> Feed</a>
<a href="/logout" class="w3-bar-item w3-button"><i class="fa fa-sign-out" aria-hidden="true"></i> Log out</a>
</div>
{{/if}}

<div id="main">
{{>_navbar}}
Expand Down
5 changes: 4 additions & 1 deletion views/login/login.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@
<span class="psw">Forgot <a href="#">password?</a></span>
</div>
</form>
</div>
</div>
<script>
sessionStorage.setItem("openSidebar", false);
</script>
Loading

0 comments on commit eb48bef

Please sign in to comment.