mirror of
https://github.com/wisplite/raster.git
synced 2026-05-01 06:32:44 -05:00
update authorization tokens and add/fix create album endpoint
This commit is contained in:
@@ -22,6 +22,7 @@ func Init() bool {
|
||||
&models.Album{},
|
||||
&models.User{},
|
||||
&models.AccessToken{},
|
||||
&models.UserAlbumAccess{},
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal("failed to migrate database: ", err)
|
||||
|
||||
@@ -19,7 +19,7 @@ type Album struct {
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type UserAccess struct {
|
||||
type UserAlbumAccess struct {
|
||||
UserID string `gorm:"not null"`
|
||||
AlbumID string `gorm:"not null"`
|
||||
AccessLevel int `gorm:"not null"` // 0: View, 1: Upload, 2: Edit, 3: Edit/Delete, 4: Admin (manage other users)
|
||||
|
||||
@@ -40,6 +40,10 @@ func RegisterAlbumRoutes(rg *gin.RouterGroup) {
|
||||
Description string `json:"description"`
|
||||
ParentID string `json:"parentId"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
result, err := services.CreateAlbum(accessToken, request.Title, request.Description, request.ParentID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
|
||||
@@ -62,11 +62,11 @@ func RegisterUserRoutes(rg *gin.RouterGroup) {
|
||||
})
|
||||
user.GET("/getUserData", func(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
token := authHeader
|
||||
if len(authHeader) > 7 && authHeader[:7] == "Bearer " {
|
||||
token = authHeader[7:]
|
||||
if authHeader == "" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
userData, err := services.GetUserData(token)
|
||||
userData, err := services.GetUserData(authHeader)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -82,7 +82,7 @@ func CreateAlbum(accessToken string, title string, description string, parentID
|
||||
}
|
||||
|
||||
func CheckUserAlbumAccess(userID string, albumID string) (int, error) {
|
||||
userAccess := models.UserAccess{}
|
||||
userAccess := models.UserAlbumAccess{}
|
||||
result := db.GetDB().First(&userAccess, "user_id = ? AND album_id = ?", userID, albumID)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
|
||||
Reference in New Issue
Block a user