Merging with dev/cedric
This commit is contained in:
58
backend/api/blogs/blogs.go
Normal file
58
backend/api/blogs/blogs.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package blogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleBlog(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
blog_uuid := r.PathValue("uuid")
|
||||
|
||||
var blog core.Blog
|
||||
_, err := core.DB.NewSelect().
|
||||
Model(&blog).
|
||||
Where("blog_id = ?", blog_uuid).
|
||||
Relation("Author").
|
||||
ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Status OK",
|
||||
Data: blog,
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
func HandleGetBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
var blog []core.Blog
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&blog).
|
||||
Relation("Author").
|
||||
ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: fmt.Sprint("%d blogs objects sent", count),
|
||||
Data: blog,
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
@@ -1,14 +1,22 @@
|
||||
package api
|
||||
package blogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"io"
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleCreateBlog(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
var blog core.Blog
|
||||
if err := json.NewDecoder(r.Body).Decode(&blog); err != nil {
|
||||
core.JSONError{
|
||||
@@ -27,13 +27,6 @@ func (dsn *DSN) ToString() string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName)
|
||||
}
|
||||
|
||||
type Role string
|
||||
|
||||
const (
|
||||
AdminRole Role = "admin"
|
||||
UserRole Role = "user"
|
||||
)
|
||||
|
||||
type Status string
|
||||
|
||||
const (
|
||||
@@ -41,20 +34,35 @@ const (
|
||||
Inactive Status = "Inactive"
|
||||
)
|
||||
|
||||
type Group string
|
||||
|
||||
const (
|
||||
LatosaGroup Group = "latosa"
|
||||
WingTsunGroup Group = "wing-tsun"
|
||||
)
|
||||
|
||||
type UserAttributes struct {
|
||||
Groups []Group `json:"groups"`
|
||||
}
|
||||
|
||||
type PermissionConditions struct {
|
||||
Groups *[]Group `json:"groups,omitempty"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
bun.BaseModel `bun:"table:users"`
|
||||
|
||||
UserID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"userId"`
|
||||
FirstName string `bun:"firstname,notnull" json:"firstname"`
|
||||
LastName string `bun:"lastname,notnull" json:"lastname"`
|
||||
Email string `bun:"email,unique,notnull" json:"email"`
|
||||
Password string `bun:"password,notnull" json:"password,omitempty"`
|
||||
Phone string `bun:"phone,notnull" json:"phone"`
|
||||
Role Role `bun:"role,notnull,default:'user'" json:"role"`
|
||||
CreatedAt time.Time `bun:"created_at,default:current_timestamp" json:"createdAt"`
|
||||
UpdatedAt time.Time `bun:"updated_at,default:current_timestamp" json:"updatedAt"`
|
||||
Events []Event `bun:"m2m:events_to_users,join:User=Event" json:"events,omitempty"`
|
||||
Articles []*Blog `bun:"rel:has-many,join:user_id=blog_id" json:"articles,omitempty"`
|
||||
UserID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"userId"`
|
||||
FirstName string `bun:"firstname,notnull" json:"firstname"`
|
||||
LastName string `bun:"lastname,notnull" json:"lastname"`
|
||||
Email string `bun:"email,unique,notnull" json:"email"`
|
||||
Password string `bun:"password,notnull" json:"password,omitempty"`
|
||||
Phone string `bun:"phone,notnull" json:"phone"`
|
||||
CreatedAt time.Time `bun:"created_at,default:current_timestamp" json:"createdAt"`
|
||||
UpdatedAt time.Time `bun:"updated_at,default:current_timestamp" json:"updatedAt"`
|
||||
Events []Event `bun:"m2m:events_to_users,join:User=Event" json:"events,omitempty"`
|
||||
Articles []*Blog `bun:"rel:has-many,join:user_id=blog_id" json:"articles,omitempty"`
|
||||
Attributes UserAttributes `bun:"attributes,type:jsonb" json:"attributes"`
|
||||
}
|
||||
|
||||
func (u *User) Insert(ctx context.Context) (sql.Result, error) {
|
||||
@@ -66,15 +74,6 @@ func (u *User) Insert(ctx context.Context) (sql.Result, error) {
|
||||
}
|
||||
|
||||
func Verify(ctx context.Context, email, password string) (*User, error) {
|
||||
// var user User
|
||||
// query := `
|
||||
// SELECT *
|
||||
// FROM users
|
||||
// WHERE email = ? AND password = crypt(?, password)
|
||||
// `
|
||||
//
|
||||
// err := DB.NewRaw(query, email, password).Scan(ctx, user)
|
||||
|
||||
var user User
|
||||
count, err := DB.NewSelect().
|
||||
Model(&user).
|
||||
@@ -94,6 +93,40 @@ func Verify(ctx context.Context, email, password string) (*User, error) {
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
type Permission struct {
|
||||
bun.BaseModel `bun:"table:permissions"`
|
||||
ID int `bun:"id,pk,autoincrement" json:"id"`
|
||||
Resource string `bun:"resource,notnull" json:"resource"`
|
||||
Action string `bun:"action,notnull" json:"action"`
|
||||
Conditions PermissionConditions `bun:"conditions,type:jsonb" json:"conditions"`
|
||||
}
|
||||
|
||||
type Role struct {
|
||||
bun.BaseModel `bun:"table:roles"`
|
||||
ID uuid.UUID `bun:"id,pk,type:uuid,default:gen_random_uuid()" json:"id"`
|
||||
Name string `bun:"name,unique,notnull" json:"name"`
|
||||
}
|
||||
|
||||
type PermissionToRole struct {
|
||||
bun.BaseModel `bun:"table:permissions_to_users"`
|
||||
|
||||
PermissionID int `bun:"permission_id,pk"`
|
||||
RoleID uuid.UUID `bun:"type:uuid,pk"`
|
||||
|
||||
Permission *Permission `bun:"rel:belongs-to,join:permission_id=id"`
|
||||
Role *Role `bun:"rel:belongs-to,join:role_id=id"`
|
||||
}
|
||||
|
||||
type UserToRole struct {
|
||||
bun.BaseModel `bun:"table:users_to_roles"`
|
||||
|
||||
UserID uuid.UUID `bun:"user_id,type:uuid,pk"`
|
||||
RoleID uuid.UUID `bun:"type:uuid,pk"`
|
||||
|
||||
User *User `bun:"rel:belongs-to,join:user_id=user_id"`
|
||||
Role *Role `bun:"rel:belongs-to,join:role_id=id"`
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
bun.BaseModel `bun:"table:events"`
|
||||
|
||||
@@ -188,6 +221,8 @@ func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
return nil, err
|
||||
}
|
||||
db.RegisterModel((*EventToUser)(nil))
|
||||
db.RegisterModel((*PermissionToRole)(nil))
|
||||
db.RegisterModel((*UserToRole)(nil))
|
||||
_, err = db.NewCreateTable().Model((*User)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Event)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*EventToUser)(nil)).IfNotExists().Exec(ctx)
|
||||
@@ -195,6 +230,10 @@ func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
_, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Media)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Shortcode)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Role)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Permission)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*PermissionToRole)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*UserToRole)(nil)).IfNotExists().Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDeleteEvent(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("event_uuid")
|
||||
var event core.Event
|
||||
res, err := core.DB.NewDelete().
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetEvent(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleEvent(w http.ResponseWriter, r *http.Request) {
|
||||
event_uuid := r.PathValue("event_uuid")
|
||||
var event core.Event
|
||||
_, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background())
|
||||
@@ -28,7 +28,7 @@ func HandleGetEvent(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
func HangleGetEvents(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleEvents(w http.ResponseWriter, r *http.Request) {
|
||||
var events []core.Event
|
||||
rowsCount, err := core.DB.NewSelect().Model(&events).ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
@@ -38,7 +38,7 @@ func HangleGetEvents(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: fmt.Sprintf("%d Event successfully sent", rowsCount),
|
||||
@@ -46,4 +46,3 @@ func HangleGetEvents(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
var event core.Event
|
||||
err := json.NewDecoder(r.Body).Decode(&event)
|
||||
if err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleUpdateEvent(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var event core.Event
|
||||
err := json.NewDecoder(r.Body).Decode(&event)
|
||||
if err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package blogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -8,10 +8,12 @@ import (
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleBlog(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
blog_uuid := r.PathValue("uuid")
|
||||
|
||||
var blog core.Blog
|
||||
|
||||
var blog core.Blog
|
||||
_, err := core.DB.NewSelect().
|
||||
Model(&blog).
|
||||
Where("blog_id = ?", blog_uuid).
|
||||
@@ -19,18 +21,18 @@ func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
||||
ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNotAcceptable)
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Status OK",
|
||||
Data: blog,
|
||||
Data: blog,
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
func HandleGetBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDeleteMedia(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
res, err := core.DB.NewDelete().
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
func HandleGetMedia(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||
queryParams := r.URL.Query()
|
||||
page, err := strconv.Atoi(queryParams.Get("page"))
|
||||
limit, err := strconv.Atoi(queryParams.Get("limit"))
|
||||
@@ -69,7 +69,7 @@ func HandleGetMedia(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
func HandleGetMediaDetails(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleMediaDetails(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
err := core.DB.NewSelect().
|
||||
@@ -96,7 +96,7 @@ func HandleGetMediaDetails(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
func HandleGetMediaFile(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleMediaFile(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
err := core.DB.NewSelect().
|
||||
3
backend/api/media/update.go
Normal file
3
backend/api/media/update.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package media
|
||||
|
||||
// TODO
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleUploadMedia(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
// Parse the multipart form
|
||||
err := r.ParseMultipartForm(10 << 20) // Limit file size to 10 MB
|
||||
if err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package media
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -18,7 +18,7 @@ type FileArgs struct {
|
||||
SizeByte int64 `json:"size"`
|
||||
}
|
||||
|
||||
func HandleVerifyMedia(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleVerify(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
@@ -2,99 +2,33 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/api/users"
|
||||
"fr.latosa-escrima/utils"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
var MySigningKey = []byte("COUCOU")
|
||||
|
||||
type LoginArgs struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type Claims struct {
|
||||
UserID string `json:"user_id"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Body == nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "No body has been provided.",
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
fmt.Println(body)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
var login LoginArgs
|
||||
err = json.Unmarshal(body, &login)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := core.Verify(context.Background(), login.Email, login.Password)
|
||||
if user == nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "User not found.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
claims := Claims{
|
||||
UserID: user.UserID.String(),
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Issuer: "latosa-escrima.fr",
|
||||
Subject: "authentification",
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
signed, err := token.SignedString(MySigningKey)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "JWT Created",
|
||||
Data: signed,
|
||||
}.Respond(w, http.StatusCreated)
|
||||
func CORS(next http.Handler) http.Handler {
|
||||
CORS_AllowOrigin := os.Getenv("CORS_AllowOrigin")
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Allow all origins (can restrict to specific origins)
|
||||
w.Header().Set("Access-Control-Allow-Origin", CORS_AllowOrigin)
|
||||
// Allow certain HTTP methods (you can customize these as needed)
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
|
||||
// Allow certain headers (you can add more as needed)
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-CSRF-Token")
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
// Handle OPTIONS pre-flight request
|
||||
if r.Method == http.MethodOptions {
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func AuthJWT(next http.Handler) http.Handler {
|
||||
@@ -125,7 +59,7 @@ func AuthJWT(next http.Handler) http.Handler {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return MySigningKey, nil
|
||||
return users.MySigningKey, nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
1
backend/api/permissions/delete.go
Normal file
1
backend/api/permissions/delete.go
Normal file
@@ -0,0 +1 @@
|
||||
package permissions
|
||||
1
backend/api/permissions/new.go
Normal file
1
backend/api/permissions/new.go
Normal file
@@ -0,0 +1 @@
|
||||
package permissions
|
||||
1
backend/api/permissions/permission.go
Normal file
1
backend/api/permissions/permission.go
Normal file
@@ -0,0 +1 @@
|
||||
package permissions
|
||||
1
backend/api/permissions/permissions.go
Normal file
1
backend/api/permissions/permissions.go
Normal file
@@ -0,0 +1 @@
|
||||
package permissions
|
||||
1
backend/api/permissions/update.go
Normal file
1
backend/api/permissions/update.go
Normal file
@@ -0,0 +1 @@
|
||||
package permissions
|
||||
1
backend/api/roles/add_permission.go
Normal file
1
backend/api/roles/add_permission.go
Normal file
@@ -0,0 +1 @@
|
||||
package roles
|
||||
1
backend/api/roles/delete.go
Normal file
1
backend/api/roles/delete.go
Normal file
@@ -0,0 +1 @@
|
||||
package roles
|
||||
1
backend/api/roles/new.go
Normal file
1
backend/api/roles/new.go
Normal file
@@ -0,0 +1 @@
|
||||
package roles
|
||||
1
backend/api/roles/permissions.go
Normal file
1
backend/api/roles/permissions.go
Normal file
@@ -0,0 +1 @@
|
||||
package roles
|
||||
1
backend/api/roles/remove_permission.go
Normal file
1
backend/api/roles/remove_permission.go
Normal file
@@ -0,0 +1 @@
|
||||
package roles
|
||||
1
backend/api/roles/role.go
Normal file
1
backend/api/roles/role.go
Normal file
@@ -0,0 +1 @@
|
||||
package roles
|
||||
1
backend/api/roles/roles.go
Normal file
1
backend/api/roles/roles.go
Normal file
@@ -0,0 +1 @@
|
||||
package roles
|
||||
1
backend/api/roles/update.go
Normal file
1
backend/api/roles/update.go
Normal file
@@ -0,0 +1 @@
|
||||
package roles
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDeleteShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.PathValue("shortcode")
|
||||
_, err := core.DB.NewDelete().
|
||||
Model((*core.Shortcode)(nil)).
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleCreateShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
var shortcode core.Shortcode
|
||||
err := json.NewDecoder(r.Body).Decode(&shortcode)
|
||||
if err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.PathValue("shortcode")
|
||||
var shortcode core.Shortcode
|
||||
err := core.DB.NewSelect().
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetShortcodes(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleShortcodes(w http.ResponseWriter, r *http.Request) {
|
||||
var shortcodes []core.Shortcode
|
||||
err := core.DB.NewSelect().Model(&shortcodes).Scan(context.Background())
|
||||
if err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -20,7 +20,7 @@ type UpdateShortcodeArgs struct {
|
||||
MediaID *uuid.UUID `json:"media_id,omitempty"` // Nullable reference to another table's ID
|
||||
}
|
||||
|
||||
func HandleUpdateShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var updateArgs UpdateShortcodeArgs
|
||||
err := json.NewDecoder(r.Body).Decode(&updateArgs)
|
||||
if err != nil {
|
||||
96
backend/api/users/auth.go
Normal file
96
backend/api/users/auth.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
var MySigningKey = []byte("COUCOU")
|
||||
|
||||
type LoginArgs struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type Claims struct {
|
||||
UserID string `json:"user_id"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Body == nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "No body has been provided.",
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
fmt.Println(body)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
var login LoginArgs
|
||||
err = json.Unmarshal(body, &login)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := core.Verify(context.Background(), login.Email, login.Password)
|
||||
if user == nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "User not found.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
claims := Claims{
|
||||
UserID: user.UserID.String(),
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Issuer: "latosa-escrima.fr",
|
||||
Subject: "authentification",
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
signed, err := token.SignedString(MySigningKey)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "JWT Created",
|
||||
Data: signed,
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("user_uuid")
|
||||
_, err := core.DB.NewDelete().
|
||||
Model((*core.User)(nil)).
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package users
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
func HandleGetMe(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleMe(w http.ResponseWriter, r *http.Request) {
|
||||
token, ok := r.Context().Value("token").(*jwt.Token)
|
||||
if !ok {
|
||||
core.JSONError{
|
||||
@@ -29,5 +29,5 @@ func HandleGetMe(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := claims["user_id"].(string)
|
||||
|
||||
r.SetPathValue("user_uuid", uuid)
|
||||
HandleGetUser(w, r)
|
||||
HandleUser(w, r)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
var user core.User
|
||||
err := json.NewDecoder(r.Body).Decode(&user)
|
||||
if err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -13,15 +13,15 @@ import (
|
||||
)
|
||||
|
||||
type UpdateUserArgs struct {
|
||||
FirstName *string `json:"firstname,omitempty"`
|
||||
LastName *string `json:"lastname,omitempty"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
Password *string `json:"password,omitempty"`
|
||||
Phone *string `json:"phone,omitempty"`
|
||||
Role *core.Role `json:"role,omitempty"`
|
||||
FirstName *string `json:"firstname,omitempty"`
|
||||
LastName *string `json:"lastname,omitempty"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
Password *string `json:"password,omitempty"`
|
||||
Phone *string `json:"phone,omitempty"`
|
||||
Attributes *core.UserAttributes `json:"attributes"`
|
||||
}
|
||||
|
||||
func HandleUpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var updateArgs UpdateUserArgs
|
||||
err := json.NewDecoder(r.Body).Decode(&updateArgs)
|
||||
if err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetUser(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleUser(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("user_uuid")
|
||||
var user core.User
|
||||
count, err := core.DB.NewSelect().
|
||||
@@ -1,4 +1,4 @@
|
||||
package api
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
func HandleGetUsers(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleUsers(w http.ResponseWriter, r *http.Request) {
|
||||
var users []core.User
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&users).
|
||||
@@ -20,9 +20,9 @@ func HandleGetUsers(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
|
||||
if count == 0 {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Not users.",
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "No users.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
@@ -35,7 +35,6 @@ func HandleGetUsers(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO : Remove password
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Users found.",
|
||||
Reference in New Issue
Block a user