Skip to content

Commit

Permalink
feat: add admin role check for deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 7, 2024
1 parent a5a8743 commit 1ae3524
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion client/components/app/Comment/Dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function Dropdown({ comment, setComment, onDelete }) {
return (
<Dialog open={isModalOpen} onOpenChange={setIsModalOpen}>
<DropdownMenu>
{user.id === comment.author.id && (
{(user.id === comment.author.id || user.role === 'admin' ) && (
<DropdownMenuTrigger asChild>
<Button variant='ghost' size='icon'>
<Ellipsis className='w-4 h-4' />
Expand Down
2 changes: 1 addition & 1 deletion client/components/app/Post/Dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default function Dropdown({ post, setPost, onDelete }) {
çöpe git
</Link>
</DropdownMenuItem>
{user.id === post.author.id && (
{(user.id === post.author.id || user.role === 'admin') && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem>
Expand Down
8 changes: 5 additions & 3 deletions server/handlers/comments/delete_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ func DeleteComment(c *fiber.Ctx) error {
}

if comment.Author != user.ID {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"message": "User not authorized.",
})
if user.Role != "admin" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"message": "User not authorized.",
})
}
}

if err := models.DeleteComment(commentID); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions server/handlers/posts/delete_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ func DeletePost(c *fiber.Ctx) error {
}

if post.Author != user.ID {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"message": "User not authorized.",
})
if user.Role != "admin" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"message": "User not authorized.",
})
}
}

if err := models.DeletePost(postID); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type User struct {
ID primitive.ObjectID `bson:"_id" json:"id"`
CreatedAt primitive.Timestamp `bson:"created_at" json:"created_at"`
IsBanned bool `bson:"is_banned" json:"is_banned,omitempty"`
IsAdmin bool `bson:"is_admin" json:"is_admin,omitempty"`
Role string `bson:"role" json:"role,omitempty"`
Email string `bson:"email,omitempty" json:"email,omitempty"`
Name string `bson:"name" json:"name"`
Username string `bson:"username" json:"username"`
Expand Down Expand Up @@ -80,7 +80,7 @@ func CreateUser(user User) error {
T: uint32(time.Now().Unix()),
}
user.IsBanned = false
user.IsAdmin = false
user.Role = "user"
user.About = ""
user.Points = 1

Expand Down

0 comments on commit 1ae3524

Please sign in to comment.