Merge pull request #4 from Nekiiinkognito/feature/routesRevamp
Feature/routes revamp
This commit is contained in:
commit
659b1eb2eb
30
enshi_back/ABAC/GlobalRules/AuthorizedRule.go
Normal file
30
enshi_back/ABAC/GlobalRules/AuthorizedRule.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package globalrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"enshi/auth"
|
||||||
|
"enshi/global"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AuthorizedRule(c *gin.Context) (bool, []error) {
|
||||||
|
cookies := c.Request.CookiesNamed("auth_cookie")
|
||||||
|
if len(cookies) == 0 {
|
||||||
|
return false, []error{fmt.Errorf("no cookies provided")}
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenFromCookies := cookies[0].Value
|
||||||
|
cookieClimes, err := auth.ValidateToken(tokenFromCookies)
|
||||||
|
if err != nil {
|
||||||
|
c.IndentedJSON(http.StatusUnauthorized, gin.H{"error auth": err.Error()})
|
||||||
|
c.Abort()
|
||||||
|
return false, []error{err}
|
||||||
|
} else {
|
||||||
|
c.Set(global.ContextUserId, cookieClimes["id"])
|
||||||
|
c.Set(global.ContextTokenData, cookieClimes)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
33
enshi_back/ABAC/GlobalRules/IsAdminRule.go
Normal file
33
enshi_back/ABAC/GlobalRules/IsAdminRule.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package globalrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
db_repo "enshi/db/go_queries"
|
||||||
|
"enshi/db_connection"
|
||||||
|
"enshi/middleware/getters"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsAdminRule(c *gin.Context) (bool, []error) {
|
||||||
|
contextUserId, err := getters.GetUserIdFromContext(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err :=
|
||||||
|
db_repo.New(db_connection.Dbx).
|
||||||
|
GetUserById(context.Background(), contextUserId)
|
||||||
|
|
||||||
|
if err != nil || user.UserID == 0 {
|
||||||
|
return false, []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !user.IsAdmin {
|
||||||
|
return false, []error{fmt.Errorf("not admin")}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
39
enshi_back/ABAC/GlobalRules/IsOwnerOfTheBlogRule.go
Normal file
39
enshi_back/ABAC/GlobalRules/IsOwnerOfTheBlogRule.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package globalrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
db_repo "enshi/db/go_queries"
|
||||||
|
"enshi/db_connection"
|
||||||
|
"enshi/middleware/getters"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsOwnerOfTheBlogRule(c *gin.Context) (bool, []error) {
|
||||||
|
blogId, err := getters.GetInt64Param(c, "blog-id")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
contextUserId, err := getters.GetUserIdFromContext(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
blog, err :=
|
||||||
|
db_repo.New(db_connection.Dbx).
|
||||||
|
GetBlogByBlogId(context.Background(), blogId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
if blog.UserID != contextUserId {
|
||||||
|
return false, []error{fmt.Errorf("now owner of the blog")}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
39
enshi_back/ABAC/GlobalRules/IsOwnerOfThePostRule.go
Normal file
39
enshi_back/ABAC/GlobalRules/IsOwnerOfThePostRule.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package globalrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
db_repo "enshi/db/go_queries"
|
||||||
|
"enshi/db_connection"
|
||||||
|
"enshi/middleware/getters"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsOwnerOfThePostRule(c *gin.Context) (bool, []error) {
|
||||||
|
postId, err := getters.GetInt64Param(c, "post-id")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
contextUserId, err := getters.GetUserIdFromContext(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
post, err :=
|
||||||
|
db_repo.New(db_connection.Dbx).
|
||||||
|
GetPostsByPostId(context.Background(), postId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
if post.UserID != contextUserId {
|
||||||
|
return false, []error{fmt.Errorf("now owner of the post")}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
40
enshi_back/ABAC/PostsPolicies/postPolicy.go
Normal file
40
enshi_back/ABAC/PostsPolicies/postPolicy.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package postspolicies
|
||||||
|
|
||||||
|
import (
|
||||||
|
"enshi/ABAC/PostsPolicies/postRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DELETE_POST = "delete_post"
|
||||||
|
UPDATE_POST = "update_post"
|
||||||
|
CREATE_POST = "create_post"
|
||||||
|
GET_POST = "get_post"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PostsPolicies(c *gin.Context) (bool, []error) {
|
||||||
|
target, exists := c.Get("target")
|
||||||
|
if !exists {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Permit if one permit
|
||||||
|
switch target {
|
||||||
|
case DELETE_POST:
|
||||||
|
return rules.CheckRule(c, postRules.DeleteRule)
|
||||||
|
|
||||||
|
case UPDATE_POST:
|
||||||
|
return rules.CheckRule(c, postRules.PostUpdateRule)
|
||||||
|
|
||||||
|
case GET_POST:
|
||||||
|
return rules.CheckRule(c, postRules.PostReadRule)
|
||||||
|
|
||||||
|
case CREATE_POST:
|
||||||
|
return rules.CheckRule(c, postRules.PostCreateRule)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
23
enshi_back/ABAC/PostsPolicies/postRules/createRule.go
Normal file
23
enshi_back/ABAC/PostsPolicies/postRules/createRule.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package postRules
|
||||||
|
|
||||||
|
import (
|
||||||
|
globalrules "enshi/ABAC/GlobalRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Only owner of the post can change it
|
||||||
|
func PostCreateRule(c *gin.Context) (bool, []error) {
|
||||||
|
rulesToCheck := []rules.RuleFunction{
|
||||||
|
globalrules.AuthorizedRule,
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := rules.CheckRules(
|
||||||
|
c,
|
||||||
|
rulesToCheck,
|
||||||
|
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||||
|
)
|
||||||
|
|
||||||
|
return isAllowed, errors
|
||||||
|
}
|
||||||
27
enshi_back/ABAC/PostsPolicies/postRules/deleteRule.go
Normal file
27
enshi_back/ABAC/PostsPolicies/postRules/deleteRule.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package postRules
|
||||||
|
|
||||||
|
import (
|
||||||
|
globalrules "enshi/ABAC/GlobalRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
const RULES_NUMBER_TO_COMPLETE = 2
|
||||||
|
|
||||||
|
// Only owner or admin can delete post
|
||||||
|
func DeleteRule(c *gin.Context) (bool, []error) {
|
||||||
|
rulesToCheck := []rules.RuleFunction{
|
||||||
|
globalrules.AuthorizedRule,
|
||||||
|
globalrules.IsOwnerOfThePostRule,
|
||||||
|
globalrules.IsAdminRule,
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := rules.CheckRules(
|
||||||
|
c,
|
||||||
|
rulesToCheck,
|
||||||
|
RULES_NUMBER_TO_COMPLETE,
|
||||||
|
)
|
||||||
|
|
||||||
|
return isAllowed, errors
|
||||||
|
}
|
||||||
10
enshi_back/ABAC/PostsPolicies/postRules/readRule.go
Normal file
10
enshi_back/ABAC/PostsPolicies/postRules/readRule.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package postRules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Only owner of the post can change it
|
||||||
|
func PostReadRule(c *gin.Context) (bool, []error) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
23
enshi_back/ABAC/PostsPolicies/postRules/updateRule.go
Normal file
23
enshi_back/ABAC/PostsPolicies/postRules/updateRule.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package postRules
|
||||||
|
|
||||||
|
import (
|
||||||
|
globalrules "enshi/ABAC/GlobalRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Only owner of the post can change it
|
||||||
|
func PostUpdateRule(c *gin.Context) (bool, []error) {
|
||||||
|
rulesToCheck := []rules.RuleFunction{
|
||||||
|
globalrules.AuthorizedRule,
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := rules.CheckRules(
|
||||||
|
c,
|
||||||
|
rulesToCheck,
|
||||||
|
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||||
|
)
|
||||||
|
|
||||||
|
return isAllowed, errors
|
||||||
|
}
|
||||||
31
enshi_back/ABAC/ProfilePolicies/ProfilePolicies.go
Normal file
31
enshi_back/ABAC/ProfilePolicies/ProfilePolicies.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package profilepolicies
|
||||||
|
|
||||||
|
import (
|
||||||
|
profilesrules "enshi/ABAC/ProfilePolicies/ProfilesRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
RESET_PROFILE = "reset_profile"
|
||||||
|
UPDATE_PROFILE = "update_profile"
|
||||||
|
CREATE_PROFILE = "create_profile"
|
||||||
|
GET_PROFILE = "get_profile"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ProfilePolicies(c *gin.Context) (bool, []error) {
|
||||||
|
target, exists := c.Get("target")
|
||||||
|
if !exists {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Permit if one permit
|
||||||
|
switch target {
|
||||||
|
case UPDATE_PROFILE:
|
||||||
|
return rules.CheckRule(c, profilesrules.UpdateProfileRule)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
22
enshi_back/ABAC/ProfilePolicies/ProfilesRules/UpdateRule.go
Normal file
22
enshi_back/ABAC/ProfilePolicies/ProfilesRules/UpdateRule.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package profilesrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
globalrules "enshi/ABAC/GlobalRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UpdateProfileRule(c *gin.Context) (bool, []error) {
|
||||||
|
rulesToCheck := []rules.RuleFunction{
|
||||||
|
globalrules.AuthorizedRule,
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := rules.CheckRules(
|
||||||
|
c,
|
||||||
|
rulesToCheck,
|
||||||
|
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||||
|
)
|
||||||
|
|
||||||
|
return isAllowed, errors
|
||||||
|
}
|
||||||
40
enshi_back/ABAC/blogsPolicies/blogPolicies.go
Normal file
40
enshi_back/ABAC/blogsPolicies/blogPolicies.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package blogspolicies
|
||||||
|
|
||||||
|
import (
|
||||||
|
blogrules "enshi/ABAC/blogsPolicies/blogRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DELETE_BLOG = "delete_blog"
|
||||||
|
UPDATE_BLOG = "update_blog"
|
||||||
|
CREATE_BLOG = "create_blog"
|
||||||
|
GET_BLOG = "get_blog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BlogPolicies(c *gin.Context) (bool, []error) {
|
||||||
|
target, exists := c.Get("target")
|
||||||
|
if !exists {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Permit if one permit
|
||||||
|
switch target {
|
||||||
|
case DELETE_BLOG:
|
||||||
|
return rules.CheckRule(c, blogrules.BlogDeleteRule)
|
||||||
|
|
||||||
|
case UPDATE_BLOG:
|
||||||
|
return rules.CheckRule(c, blogrules.BlogUpdateRule)
|
||||||
|
|
||||||
|
case GET_BLOG:
|
||||||
|
return rules.CheckRule(c, blogrules.BlogReadRule)
|
||||||
|
|
||||||
|
case CREATE_BLOG:
|
||||||
|
return rules.CheckRule(c, blogrules.BlogCreateRule)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
22
enshi_back/ABAC/blogsPolicies/blogRules/createRule.go
Normal file
22
enshi_back/ABAC/blogsPolicies/blogRules/createRule.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package blogrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
globalrules "enshi/ABAC/GlobalRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BlogCreateRule(c *gin.Context) (bool, []error) {
|
||||||
|
rulesToCheck := []rules.RuleFunction{
|
||||||
|
globalrules.AuthorizedRule,
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := rules.CheckRules(
|
||||||
|
c,
|
||||||
|
rulesToCheck,
|
||||||
|
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||||
|
)
|
||||||
|
|
||||||
|
return isAllowed, errors
|
||||||
|
}
|
||||||
24
enshi_back/ABAC/blogsPolicies/blogRules/deleteRule.go
Normal file
24
enshi_back/ABAC/blogsPolicies/blogRules/deleteRule.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package blogrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
globalrules "enshi/ABAC/GlobalRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BlogDeleteRule(c *gin.Context) (bool, []error) {
|
||||||
|
rulesToCheck := []rules.RuleFunction{
|
||||||
|
globalrules.AuthorizedRule,
|
||||||
|
globalrules.IsOwnerOfTheBlogRule,
|
||||||
|
globalrules.IsAdminRule,
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := rules.CheckRules(
|
||||||
|
c,
|
||||||
|
rulesToCheck,
|
||||||
|
2,
|
||||||
|
)
|
||||||
|
|
||||||
|
return isAllowed, errors
|
||||||
|
}
|
||||||
19
enshi_back/ABAC/blogsPolicies/blogRules/readRule.go
Normal file
19
enshi_back/ABAC/blogsPolicies/blogRules/readRule.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package blogrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BlogReadRule(c *gin.Context) (bool, []error) {
|
||||||
|
rulesToCheck := []rules.RuleFunction{}
|
||||||
|
|
||||||
|
isAllowed, errors := rules.CheckRules(
|
||||||
|
c,
|
||||||
|
rulesToCheck,
|
||||||
|
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||||
|
)
|
||||||
|
|
||||||
|
return isAllowed, errors
|
||||||
|
}
|
||||||
23
enshi_back/ABAC/blogsPolicies/blogRules/updateRule.go
Normal file
23
enshi_back/ABAC/blogsPolicies/blogRules/updateRule.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package blogrules
|
||||||
|
|
||||||
|
import (
|
||||||
|
globalrules "enshi/ABAC/GlobalRules"
|
||||||
|
"enshi/ABAC/rules"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BlogUpdateRule(c *gin.Context) (bool, []error) {
|
||||||
|
rulesToCheck := []rules.RuleFunction{
|
||||||
|
globalrules.AuthorizedRule,
|
||||||
|
globalrules.IsOwnerOfTheBlogRule,
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := rules.CheckRules(
|
||||||
|
c,
|
||||||
|
rulesToCheck,
|
||||||
|
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||||
|
)
|
||||||
|
|
||||||
|
return isAllowed, errors
|
||||||
|
}
|
||||||
72
enshi_back/ABAC/rules/CheckRule.go
Normal file
72
enshi_back/ABAC/rules/CheckRule.go
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package rules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RuleFunction func(*gin.Context) (bool, []error)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ALL_RULES_MUST_BE_COMPLETED = iota
|
||||||
|
)
|
||||||
|
|
||||||
|
func CheckRule(
|
||||||
|
c *gin.Context,
|
||||||
|
ruleChecker RuleFunction,
|
||||||
|
) (bool, []error) {
|
||||||
|
IsAllowed, err := ruleChecker(c)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return IsAllowed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckRules(
|
||||||
|
c *gin.Context,
|
||||||
|
rules []RuleFunction,
|
||||||
|
completedRulesCount int,
|
||||||
|
) (bool, []error) {
|
||||||
|
var allowancesIndexes []int
|
||||||
|
var errors []error
|
||||||
|
|
||||||
|
if len(rules) < completedRulesCount {
|
||||||
|
return false, []error{fmt.Errorf("there is less rules, that should be completed")}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, rule := range rules {
|
||||||
|
if isAllowed, err := CheckRule(c, rule); err != nil {
|
||||||
|
errors = append(
|
||||||
|
errors,
|
||||||
|
err...,
|
||||||
|
)
|
||||||
|
} else if !isAllowed {
|
||||||
|
errors = append(
|
||||||
|
errors,
|
||||||
|
fmt.Errorf("rule "+
|
||||||
|
strconv.Itoa(i)+
|
||||||
|
" was rejected"),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
allowancesIndexes = append(allowancesIndexes, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch completedRulesCount {
|
||||||
|
case ALL_RULES_MUST_BE_COMPLETED:
|
||||||
|
if len(allowancesIndexes) == len(rules) {
|
||||||
|
return true, nil
|
||||||
|
} else {
|
||||||
|
return false, errors
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if len(allowancesIndexes) >= completedRulesCount {
|
||||||
|
return true, nil
|
||||||
|
} else {
|
||||||
|
return false, errors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -21,7 +21,7 @@ RETURNING blog_id, user_id, title, description, category_id, created_at
|
|||||||
type CreateBlogByUserIdParams struct {
|
type CreateBlogByUserIdParams struct {
|
||||||
BlogID int64 `json:"blog_id"`
|
BlogID int64 `json:"blog_id"`
|
||||||
UserID int64 `json:"user_id"`
|
UserID int64 `json:"user_id"`
|
||||||
Title pgtype.Text `json:"title"`
|
Title pgtype.Text `json:"title" validate:"required"`
|
||||||
Description pgtype.Text `json:"description"`
|
Description pgtype.Text `json:"description"`
|
||||||
CategoryID pgtype.Int4 `json:"category_id"`
|
CategoryID pgtype.Int4 `json:"category_id"`
|
||||||
}
|
}
|
||||||
@ -56,6 +56,26 @@ func (q *Queries) DeleteBlogByBlogId(ctx context.Context, blogID int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getBlogByBlogId = `-- name: GetBlogByBlogId :one
|
||||||
|
SELECT blog_id, user_id, title, description, category_id, created_at
|
||||||
|
FROM public.blogs
|
||||||
|
WHERE blog_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetBlogByBlogId(ctx context.Context, blogID int64) (Blog, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getBlogByBlogId, blogID)
|
||||||
|
var i Blog
|
||||||
|
err := row.Scan(
|
||||||
|
&i.BlogID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Title,
|
||||||
|
&i.Description,
|
||||||
|
&i.CategoryID,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const getBlogsByUserId = `-- name: GetBlogsByUserId :many
|
const getBlogsByUserId = `-- name: GetBlogsByUserId :many
|
||||||
SELECT blog_id, user_id, title, description, category_id, created_at
|
SELECT blog_id, user_id, title, description, category_id, created_at
|
||||||
FROM public.blogs
|
FROM public.blogs
|
||||||
@ -97,7 +117,7 @@ RETURNING blog_id, user_id, title, description, category_id, created_at
|
|||||||
`
|
`
|
||||||
|
|
||||||
type UpdateBlogInfoByBlogIdParams struct {
|
type UpdateBlogInfoByBlogIdParams struct {
|
||||||
Title pgtype.Text `json:"title"`
|
Title pgtype.Text `json:"title" validate:"required"`
|
||||||
Description pgtype.Text `json:"description"`
|
Description pgtype.Text `json:"description"`
|
||||||
CategoryID pgtype.Int4 `json:"category_id"`
|
CategoryID pgtype.Int4 `json:"category_id"`
|
||||||
BlogID int64 `json:"blog_id"`
|
BlogID int64 `json:"blog_id"`
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import (
|
|||||||
type Blog struct {
|
type Blog struct {
|
||||||
BlogID int64 `json:"blog_id"`
|
BlogID int64 `json:"blog_id"`
|
||||||
UserID int64 `json:"user_id"`
|
UserID int64 `json:"user_id"`
|
||||||
Title pgtype.Text `json:"title"`
|
Title pgtype.Text `json:"title" validate:"required"`
|
||||||
Description pgtype.Text `json:"description"`
|
Description pgtype.Text `json:"description"`
|
||||||
CategoryID pgtype.Int4 `json:"category_id"`
|
CategoryID pgtype.Int4 `json:"category_id"`
|
||||||
CreatedAt pgtype.Timestamp `json:"created_at"`
|
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||||
|
|||||||
@ -15,6 +15,11 @@ SELECT *
|
|||||||
FROM public.blogs
|
FROM public.blogs
|
||||||
WHERE user_id = $1;
|
WHERE user_id = $1;
|
||||||
|
|
||||||
|
-- name: GetBlogByBlogId :one
|
||||||
|
SELECT *
|
||||||
|
FROM public.blogs
|
||||||
|
WHERE blog_id = $1;
|
||||||
|
|
||||||
-- name: DeleteBlogByBlogId :exec
|
-- name: DeleteBlogByBlogId :exec
|
||||||
DELETE FROM public.blogs
|
DELETE FROM public.blogs
|
||||||
WHERE blog_id=$1;
|
WHERE blog_id=$1;
|
||||||
@ -18,6 +18,9 @@ sql:
|
|||||||
- column: users.email
|
- column: users.email
|
||||||
go_struct_tag: validate:"required,email"
|
go_struct_tag: validate:"required,email"
|
||||||
|
|
||||||
|
- column: blogs.title
|
||||||
|
go_struct_tag: validate:"required"
|
||||||
|
|
||||||
- db_type: "uuid"
|
- db_type: "uuid"
|
||||||
go_type:
|
go_type:
|
||||||
import: 'github.com/google/uuid'
|
import: 'github.com/google/uuid'
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import (
|
|||||||
"enshi/db_connection"
|
"enshi/db_connection"
|
||||||
"enshi/env"
|
"enshi/env"
|
||||||
"enshi/global"
|
"enshi/global"
|
||||||
utils "enshi/utils"
|
"enshi/routes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -27,7 +27,7 @@ func main() {
|
|||||||
defer db_connection.Dbx_connection.Close(context.Background())
|
defer db_connection.Dbx_connection.Close(context.Background())
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
if err := utils.SetupRotes(router); err != nil {
|
if err := routes.SetupRotes(router); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
40
enshi_back/middleware/ProfileMiddleware.go
Normal file
40
enshi_back/middleware/ProfileMiddleware.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
profilepolicies "enshi/ABAC/ProfilePolicies"
|
||||||
|
rest_api_stuff "enshi/REST_API_stuff"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ProfileMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
switch c.Request.Method {
|
||||||
|
case "PUT":
|
||||||
|
c.Set("target", profilepolicies.UPDATE_PROFILE)
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := profilepolicies.ProfilePolicies(c)
|
||||||
|
|
||||||
|
var errorsMap = map[int]string{}
|
||||||
|
for i, error := range errors {
|
||||||
|
errorsMap[i] = error.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors != nil {
|
||||||
|
c.IndentedJSON(http.StatusUnauthorized, errorsMap)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isAllowed {
|
||||||
|
rest_api_stuff.UnauthorizedAnswer(c, fmt.Errorf("you have no permission"))
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,7 +3,6 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
rest_api_stuff "enshi/REST_API_stuff"
|
rest_api_stuff "enshi/REST_API_stuff"
|
||||||
"enshi/middleware/checkRole"
|
"enshi/middleware/checkRole"
|
||||||
"enshi/middleware/getters"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -12,14 +11,7 @@ import (
|
|||||||
func AdminMiddleware() gin.HandlerFunc {
|
func AdminMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
|
|
||||||
userId, err := getters.GetUserIdFromContext(c)
|
isAdmin, err := checkRole.IsAdmin(c)
|
||||||
|
|
||||||
if err != nil || userId == 0 {
|
|
||||||
rest_api_stuff.BadRequestAnswer(c, err)
|
|
||||||
c.Abort()
|
|
||||||
}
|
|
||||||
|
|
||||||
isAdmin, err := checkRole.IsAdmin(userId)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rest_api_stuff.BadRequestAnswer(c, err)
|
rest_api_stuff.BadRequestAnswer(c, err)
|
||||||
|
|||||||
47
enshi_back/middleware/blogsMiddleware.go
Normal file
47
enshi_back/middleware/blogsMiddleware.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
blogspolicies "enshi/ABAC/blogsPolicies"
|
||||||
|
rest_api_stuff "enshi/REST_API_stuff"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BlogsMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
|
||||||
|
switch c.Request.Method {
|
||||||
|
case "DELETE":
|
||||||
|
c.Set("target", blogspolicies.DELETE_BLOG)
|
||||||
|
case "PUT":
|
||||||
|
c.Set("target", blogspolicies.UPDATE_BLOG)
|
||||||
|
case "POST":
|
||||||
|
c.Set("target", blogspolicies.CREATE_BLOG)
|
||||||
|
case "GET":
|
||||||
|
c.Set("target", blogspolicies.GET_BLOG)
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := blogspolicies.BlogPolicies(c)
|
||||||
|
|
||||||
|
var errorsMap = map[int]string{}
|
||||||
|
for i, error := range errors {
|
||||||
|
errorsMap[i] = error.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors != nil {
|
||||||
|
c.IndentedJSON(http.StatusUnauthorized, errorsMap)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isAllowed {
|
||||||
|
rest_api_stuff.UnauthorizedAnswer(c, fmt.Errorf("you have no permission"))
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,9 +4,18 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
db_repo "enshi/db/go_queries"
|
db_repo "enshi/db/go_queries"
|
||||||
"enshi/db_connection"
|
"enshi/db_connection"
|
||||||
|
"enshi/middleware/getters"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsAdmin(userId int64) (bool, error) {
|
func IsAdmin(c *gin.Context) (bool, error) {
|
||||||
|
userId, err := getters.GetUserIdFromContext(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
user, err :=
|
user, err :=
|
||||||
db_repo.New(db_connection.Dbx).
|
db_repo.New(db_connection.Dbx).
|
||||||
GetUserById(context.Background(), userId)
|
GetUserById(context.Background(), userId)
|
||||||
|
|||||||
@ -4,9 +4,24 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
db_repo "enshi/db/go_queries"
|
db_repo "enshi/db/go_queries"
|
||||||
"enshi/db_connection"
|
"enshi/db_connection"
|
||||||
|
"enshi/middleware/getters"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsOwnerOfThePost(userId int64, postId int64) (bool, error) {
|
func IsOwnerOfThePost(c *gin.Context) (bool, error) {
|
||||||
|
postId, err := getters.GetInt64Param(c, "post-id")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
userId, err := getters.GetUserIdFromContext(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
post, err :=
|
post, err :=
|
||||||
db_repo.New(db_connection.Dbx).
|
db_repo.New(db_connection.Dbx).
|
||||||
GetPostsByPostId(context.Background(), postId)
|
GetPostsByPostId(context.Background(), postId)
|
||||||
17
enshi_back/middleware/getters/getIntParam.go
Normal file
17
enshi_back/middleware/getters/getIntParam.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package getters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetInt64Param(c *gin.Context, paramName string) (int64, error) {
|
||||||
|
int64ParamValue, err := strconv.ParseInt(c.Param(paramName), 10, 64)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64ParamValue, nil
|
||||||
|
}
|
||||||
47
enshi_back/middleware/postsMiddleware.go
Normal file
47
enshi_back/middleware/postsMiddleware.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
postspolicies "enshi/ABAC/PostsPolicies"
|
||||||
|
rest_api_stuff "enshi/REST_API_stuff"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PostsMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
|
||||||
|
switch c.Request.Method {
|
||||||
|
case "DELETE":
|
||||||
|
c.Set("target", postspolicies.DELETE_POST)
|
||||||
|
case "PUT":
|
||||||
|
c.Set("target", postspolicies.UPDATE_POST)
|
||||||
|
case "POST":
|
||||||
|
c.Set("target", postspolicies.CREATE_POST)
|
||||||
|
case "GET":
|
||||||
|
c.Set("target", postspolicies.GET_POST)
|
||||||
|
}
|
||||||
|
|
||||||
|
isAllowed, errors := postspolicies.PostsPolicies(c)
|
||||||
|
|
||||||
|
var errorsMap = map[int]string{}
|
||||||
|
for i, error := range errors {
|
||||||
|
errorsMap[i] = error.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors != nil {
|
||||||
|
c.IndentedJSON(http.StatusUnauthorized, errorsMap)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isAllowed {
|
||||||
|
rest_api_stuff.UnauthorizedAnswer(c, fmt.Errorf("you have no permission"))
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
48
enshi_back/routes/blogRoutes/createBlog.go
Normal file
48
enshi_back/routes/blogRoutes/createBlog.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package blogRoutes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
rest_api_stuff "enshi/REST_API_stuff"
|
||||||
|
db_repo "enshi/db/go_queries"
|
||||||
|
"enshi/db_connection"
|
||||||
|
"enshi/middleware/getters"
|
||||||
|
"enshi/utils"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateBlog(c *gin.Context) {
|
||||||
|
blogParams, err := utils.GetContextPayload[db_repo.CreateBlogByUserIdParams](c)
|
||||||
|
if err != nil {
|
||||||
|
rest_api_stuff.BadRequestAnswer(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userId, err := getters.GetUserIdFromContext(c)
|
||||||
|
if err != nil {
|
||||||
|
rest_api_stuff.BadRequestAnswer(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
blogId, err := utils.GetUUIDv7AsInt64()
|
||||||
|
if err != nil {
|
||||||
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
blogParams.UserID = userId
|
||||||
|
blogParams.BlogID = blogId
|
||||||
|
|
||||||
|
_, err = db_repo.
|
||||||
|
New(db_connection.Dbx).
|
||||||
|
CreateBlogByUserId(
|
||||||
|
context.Background(),
|
||||||
|
blogParams,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rest_api_stuff.OkAnswer(c, "blog has been created")
|
||||||
|
}
|
||||||
@ -1,17 +0,0 @@
|
|||||||
package routes
|
|
||||||
|
|
||||||
import (
|
|
||||||
rest_api_stuff "enshi/REST_API_stuff"
|
|
||||||
db_repo "enshi/db/go_queries"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ChangeUserProfile(c *gin.Context) {
|
|
||||||
var userProfileParams db_repo.UpdateProfileByUserIdParams
|
|
||||||
|
|
||||||
if err := c.BindJSON(&userProfileParams); err != nil {
|
|
||||||
rest_api_stuff.BadRequestAnswer(c, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -7,16 +7,15 @@ import (
|
|||||||
"enshi/db_connection"
|
"enshi/db_connection"
|
||||||
"enshi/middleware/getters"
|
"enshi/middleware/getters"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeletePost(c *gin.Context) {
|
func DeletePost(c *gin.Context) {
|
||||||
var deletePostId struct {
|
postId, err := strconv.ParseInt(c.Param("post-id"), 10, 64)
|
||||||
PostId int64 `json:"post_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.BindJSON(&deletePostId); err != nil {
|
if err != nil {
|
||||||
rest_api_stuff.BadRequestAnswer(c, err)
|
rest_api_stuff.BadRequestAnswer(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -28,7 +27,7 @@ func DeletePost(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
query := db_repo.New(db_connection.Dbx)
|
query := db_repo.New(db_connection.Dbx)
|
||||||
post, err := query.GetPostsByPostId(context.Background(), deletePostId.PostId)
|
post, err := query.GetPostsByPostId(context.Background(), postId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||||
return
|
return
|
||||||
@ -41,7 +40,7 @@ func DeletePost(c *gin.Context) {
|
|||||||
|
|
||||||
// TODO: Add block of code, so admin could delete anything
|
// TODO: Add block of code, so admin could delete anything
|
||||||
|
|
||||||
err = query.DeletePostByPostId(context.Background(), deletePostId.PostId)
|
err = query.DeletePostByPostId(context.Background(), postId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -5,24 +5,23 @@ import (
|
|||||||
rest_api_stuff "enshi/REST_API_stuff"
|
rest_api_stuff "enshi/REST_API_stuff"
|
||||||
db_repo "enshi/db/go_queries"
|
db_repo "enshi/db/go_queries"
|
||||||
"enshi/db_connection"
|
"enshi/db_connection"
|
||||||
|
"enshi/middleware/getters"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetPost(c *gin.Context) {
|
func GetPost(c *gin.Context) {
|
||||||
var postParams struct {
|
postId, err := getters.GetInt64Param(c, "post-id")
|
||||||
PostId int64 `json:"post_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.BindJSON(&postParams); err != nil {
|
if err != nil {
|
||||||
rest_api_stuff.BadRequestAnswer(c, err)
|
rest_api_stuff.BadRequestAnswer(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
postData, err :=
|
postData, err :=
|
||||||
db_repo.New(db_connection.Dbx).
|
db_repo.New(db_connection.Dbx).
|
||||||
GetPostsByPostId(context.Background(), postParams.PostId)
|
GetPostsByPostId(context.Background(), postId)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||||
|
|||||||
@ -5,9 +5,7 @@ import (
|
|||||||
rest_api_stuff "enshi/REST_API_stuff"
|
rest_api_stuff "enshi/REST_API_stuff"
|
||||||
db_repo "enshi/db/go_queries"
|
db_repo "enshi/db/go_queries"
|
||||||
"enshi/db_connection"
|
"enshi/db_connection"
|
||||||
"enshi/middleware/checkRole"
|
|
||||||
"enshi/middleware/getters"
|
"enshi/middleware/getters"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -20,20 +18,14 @@ func UpdatePost(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userId, err := getters.GetUserIdFromContext(c)
|
postId, err := getters.GetInt64Param(c, "post-id")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isOwner, _ := checkRole.IsOwnerOfThePost(
|
UpdatedPostParams.PostID = postId
|
||||||
userId,
|
|
||||||
UpdatedPostParams.PostID,
|
|
||||||
); !isOwner {
|
|
||||||
rest_api_stuff.UnauthorizedAnswer(c, fmt.Errorf("you are now allowed to change this"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = db_repo.New(
|
_, err = db_repo.New(
|
||||||
db_connection.Dbx,
|
db_connection.Dbx,
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
package utils
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"enshi/middleware"
|
"enshi/middleware"
|
||||||
"enshi/routes"
|
|
||||||
"enshi/routes/authRoutes"
|
"enshi/routes/authRoutes"
|
||||||
|
"enshi/routes/blogRoutes"
|
||||||
"enshi/routes/postsRoutes"
|
"enshi/routes/postsRoutes"
|
||||||
"enshi/routes/userProfileRoutes"
|
"enshi/routes/userProfileRoutes"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -29,21 +29,55 @@ func SetupRotes(g *gin.Engine) error {
|
|||||||
|
|
||||||
freeGroup.GET("getCookie", testCookie)
|
freeGroup.GET("getCookie", testCookie)
|
||||||
|
|
||||||
freeGroup.POST("login", authRoutes.Login)
|
freeGroup.POST(
|
||||||
freeGroup.POST("registerUser", authRoutes.RegisterUser)
|
"login",
|
||||||
freeGroup.GET("getPost", postsRoutes.GetPost)
|
authRoutes.Login,
|
||||||
|
)
|
||||||
|
freeGroup.POST(
|
||||||
|
"users",
|
||||||
|
authRoutes.RegisterUser,
|
||||||
|
)
|
||||||
|
|
||||||
|
postsGroup := g.Group("/")
|
||||||
|
postsGroup.Use(middleware.PostsMiddleware())
|
||||||
|
|
||||||
|
postsGroup.GET(
|
||||||
|
"posts/:post-id",
|
||||||
|
postsRoutes.GetPost,
|
||||||
|
)
|
||||||
|
postsGroup.PUT(
|
||||||
|
"posts/:post-id",
|
||||||
|
postsRoutes.UpdatePost,
|
||||||
|
)
|
||||||
|
postsGroup.POST(
|
||||||
|
"posts",
|
||||||
|
postsRoutes.CreatePost,
|
||||||
|
)
|
||||||
|
postsGroup.DELETE(
|
||||||
|
"posts/:post-id",
|
||||||
|
postsRoutes.DeletePost,
|
||||||
|
)
|
||||||
|
|
||||||
|
blogGroup := g.Group("/")
|
||||||
|
blogGroup.Use(middleware.BlogsMiddleware())
|
||||||
|
|
||||||
|
blogGroup.POST(
|
||||||
|
"blogs",
|
||||||
|
blogRoutes.CreateBlog,
|
||||||
|
)
|
||||||
|
|
||||||
|
profilesGroup := g.Group("/")
|
||||||
|
profilesGroup.Use(middleware.ProfileMiddleware())
|
||||||
|
|
||||||
|
profilesGroup.PUT(
|
||||||
|
"profiles",
|
||||||
|
userProfileRoutes.UpdateUserProfile,
|
||||||
|
)
|
||||||
|
|
||||||
// Auth group routes
|
// Auth group routes
|
||||||
authGroup := g.Group("/")
|
authGroup := g.Group("/")
|
||||||
authGroup.Use(middleware.AuthMiddleware())
|
authGroup.Use(middleware.AuthMiddleware())
|
||||||
|
|
||||||
authGroup.POST("updatePost", postsRoutes.UpdatePost)
|
|
||||||
authGroup.POST("createPost", postsRoutes.CreatePost)
|
|
||||||
authGroup.POST("changeUserProfile", routes.ChangeUserProfile)
|
|
||||||
authGroup.POST("updateProfile", userProfileRoutes.UpdateUserProfile)
|
|
||||||
|
|
||||||
authGroup.DELETE("deletePost", postsRoutes.DeletePost)
|
|
||||||
|
|
||||||
// Admin group routes
|
// Admin group routes
|
||||||
adminGroup := authGroup.Group("/admin/")
|
adminGroup := authGroup.Group("/admin/")
|
||||||
adminGroup.Use(middleware.AdminMiddleware())
|
adminGroup.Use(middleware.AdminMiddleware())
|
||||||
@ -6,14 +6,16 @@ import (
|
|||||||
db_repo "enshi/db/go_queries"
|
db_repo "enshi/db/go_queries"
|
||||||
"enshi/db_connection"
|
"enshi/db_connection"
|
||||||
"enshi/middleware/getters"
|
"enshi/middleware/getters"
|
||||||
|
"enshi/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UpdateUserProfile(c *gin.Context) {
|
func UpdateUserProfile(c *gin.Context) {
|
||||||
var newProfile db_repo.UpdateProfileByUserIdParams
|
newProfile, err :=
|
||||||
|
utils.GetContextPayload[db_repo.UpdateProfileByUserIdParams](c)
|
||||||
|
|
||||||
if err := c.BindJSON(&newProfile); err != nil {
|
if err != nil {
|
||||||
rest_api_stuff.BadRequestAnswer(c, err)
|
rest_api_stuff.BadRequestAnswer(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
21
enshi_back/utils/GetContextPayload.go
Normal file
21
enshi_back/utils/GetContextPayload.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetContextPayload[T any](c *gin.Context) (T, error) {
|
||||||
|
var params T
|
||||||
|
|
||||||
|
if err := c.BindJSON(¶ms); err != nil {
|
||||||
|
return params, err
|
||||||
|
}
|
||||||
|
|
||||||
|
validate := validator.New(validator.WithRequiredStructEnabled())
|
||||||
|
if err := validate.Struct(params); err != nil {
|
||||||
|
return params, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return params, nil
|
||||||
|
}
|
||||||
18
enshi_back/utils/uuidGenerator.go
Normal file
18
enshi_back/utils/uuidGenerator.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetUUIDv7AsInt64() (int64, error) {
|
||||||
|
uuid, err := uuid.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return -int64(
|
||||||
|
binary.BigEndian.Uint64(uuid[8:]),
|
||||||
|
), nil
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user