Added user delete

This commit is contained in:
Akif9748 2022-03-13 17:12:09 +03:00
parent 6b483c8d28
commit 259ba11f0f
8 changed files with 92 additions and 42 deletions

View file

@ -24,6 +24,8 @@ A forum script written in Node.js.
- [x] Logout - [x] Logout
- [x] Admin - [x] Admin
- [x] Message count - [x] Message count
- [x] Delete User
- [ ] Singature & About me
- [ ] Messages - [ ] Messages
- [x] Send message - [x] Send message
- [x] Delete message - [x] Delete message

View file

@ -17,7 +17,7 @@ module.exports = class Thread {
} }
getId(id = this.id) { getId(id = this.id) {
const thread = db.get("threads."+id); const thread = db.get("threads").find(t => t.id == id);
if (!thread) return null; if (!thread) return null;
this.id = id; this.id = id;
const { title, author, messages = [], time = new Date().getTime(), deleted = false } = thread; const { title, author, messages = [], time = new Date().getTime(), deleted = false } = thread;
@ -27,7 +27,7 @@ module.exports = class Thread {
this.time = time; this.time = time;
this.deleted = deleted; this.deleted = deleted;
return this return this;
} }
takeId(){ takeId(){

View file

@ -3,26 +3,45 @@ const db = require("quick.db")
module.exports = class User { module.exports = class User {
constructor(name = "guest", avatar = "/images/guest.png", time = new Date().getTime(), admin= false) { constructor(name = "guest", avatar = "/images/guest.png", time = new Date().getTime(), admin = false, deleted = false) {
this.name = name; this.name = name;
this.avatar = avatar; this.avatar = avatar;
this.time = time; this.time = time;
this.admin = admin; this.admin = admin;
this.deleted = deleted;
} }
getId(id = this.id) { getId(id = this.id) {
const user = db.get("users." + id); const user = db.get("users").find(u => u.id == id);
if (!user) return null; if (!user) return null;
this.id = Number(id); this.id = Number(id);
const { name = "guest", avatar = "/images/guest.png", time = new Date().getTime(), admin = false } = user; const { name = "guest", avatar = "/images/guest.png", time = new Date().getTime(), admin = false, deleted = false } = user;
this.name = name; this.name = name;
this.avatar = avatar; this.avatar = avatar;
this.time = time; this.time = time;
this.admin = admin; this.admin = admin;
return this this.deleted = deleted;
return this ;
} }
takeId() {
getName(name1 = this.name) {
const user = db.get("users").find(u => u.name == name1);
if (!user) return null;
this.id = Number(user.id);
const { name = "guest", avatar = "/images/guest.png", time = new Date().getTime(), admin = false, deleted = false } = user;
this.name = name;
this.avatar = avatar;
this.time = time;
this.admin = admin;
this.deleted = deleted;
return this ;
}
takeId() {
let id = db.get("users"); let id = db.get("users");
this.id = id ? id.length : 0; this.id = id ? id.length : 0;
return this return this
@ -34,8 +53,6 @@ module.exports = class User {
} }
getLink(id = this.id) { getLink(id = this.id) {
return "/users/" + id; return "/users/" + id;
} }
} }

View file

@ -10,9 +10,9 @@ module.exports = (req, res) => {
if (!id) { if (!id) {
const users = db.get("users").slice(0, 10); const users = db.get("users").slice(0);
const links = users.map(user => "/users/" + user.id) const links = users.filter(user=> !user.deleted).map(user => "/users/" + user.id)
return res.render("users", { users, links, user }) return res.render("users", { users, links, user })
} }
@ -20,7 +20,7 @@ module.exports = (req, res) => {
const member = new User().getId(id); const member = new User().getId(id);
if (member) { if (member && (user.admin || !member.deleted)) {
const message = db.get("messages").filter(message => message.author.id === Number(id)).length const message = db.get("messages").filter(message => message.author.id === Number(id)).length
const thread = db.get("threads").filter(thread => thread.author.id === Number(id)).length const thread = db.get("threads").filter(thread => thread.author.id === Number(id)).length

View file

@ -1,5 +1,6 @@
const db = require("quick.db"); const db = require("quick.db");
const error = require("../../errors/error.js") const error = require("../../errors/error.js")
const { User, Message } = require("../../classes/index");
module.exports = (req, res) => { module.exports = (req, res) => {
req.session.loggedin = false; req.session.loggedin = false;
@ -12,7 +13,7 @@ module.exports = (req, res) => {
if (user) { if (user) {
// Authenticate the user // Authenticate the user
if (user.key !== password) return error(res, 404, 'Incorrect Password!') if (user.key !== password) return error(res, 404, 'Incorrect Password!')
if (new User().getName(username).deleted) return error(res, 404, 'Incorrect Username and/or Password!')
req.session.loggedin = true; req.session.loggedin = true;
req.session.username = username; req.session.username = username;
req.session.userid = user.id; req.session.userid = user.id;

18
routes/post/userDelete.js Normal file
View file

@ -0,0 +1,18 @@
const { User, Message } = require("../../classes/index");
const error = require("../../errors/error.js");
module.exports = (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid);
if (!user.admin)
return error(res, 403, "You have not got permission for this.");
const id = req.url.slice(9 + 3)
const member = new User().getId(id);
if (!member || member.deleted) return error(res, 404, "We have not got any user declared as this id.");
member.deleted = true;
member.write();
res.redirect("/admin");
}

View file

@ -36,6 +36,7 @@
<hr> <hr>
<button class="buyuk" type="submit">Make admin!</button> <button class="buyuk" type="submit">Make admin!</button>
</form> </form>
<script> <script>
if (<%= user2.admin %>) if (<%= user2.admin %>)
alert("Making admin of '<%= user2.name %>'' is success"); alert("Making admin of '<%= user2.name %>'' is success");

View file

@ -31,38 +31,49 @@
<!-- Navbar end --> <!-- Navbar end -->
<ul> <ul>
<li> <li>
<h1>Avatar:</h1> <h1>Avatar:</h1>
<img style="width:256px;height:256px;" src=<%=member.avatar %> alt=<%= member.name %>> <img style="width:256px;height:256px;" src=<%=member.avatar %> alt=<%= member.name %>>
</li> </li>
<li> <li>
<h2>Name: <%= member.name %> <h2>Name: <%= member.name %>
</h2> </h2>
</li> </li>
<li> <li>
<h2>Created at: <h2>Created at:
<%= new Date(member.time).toLocaleString() %> <%= new Date(member.time).toLocaleString() %>
</h2> </h2>
</li> </li>
<li> <li>
<h2>Is admin? <%= member.admin ? "Yes" : "No" %> <h2>Is admin? <%= member.admin ? "Yes" : "No" %>
</h2> </h2>
</li> </li>
<li> <li>
<h2> Message: <%= counts.message %> <h2> Message: <%= counts.message %>
</h2> </h2>
</li> </li>
<li> <li>
<h2> Thread: <%= counts.thread %> <h2> Thread: <%= counts.thread %>
</h2> </h2>
</li> </li>
</ul> </ul>
<% if (user.admin) {%>
<form action="/admin/" method="POST">
<input name="userid" type="hidden" value="<%= member.id %>"></input>
<button class="buyuk" type="submit">Make admin!</button>
</form>
<form action="/userDelete/<%= member.id %>" method="POST">
<button class="buyuk" type="submit">Delete user!</button>
</form>
<% }; %>
</body> </body>