mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 09:32:50 +00:00
use permissions from database - updated bot-scripts
This commit is contained in:
@@ -31,22 +31,12 @@ type CustomClaims struct {
|
||||
// GetJWT - get json web token
|
||||
func GetJWT(user model.User) (string, error) {
|
||||
|
||||
permissions := PermUser
|
||||
|
||||
if checkEmailPermissions(user.Email, config.Config.ModEmails) {
|
||||
permissions = PermMod
|
||||
}
|
||||
|
||||
if checkEmailPermissions(user.Email, config.Config.AdminEmails) {
|
||||
permissions = PermAdmin
|
||||
}
|
||||
|
||||
claims := CustomClaims{
|
||||
user.ID,
|
||||
user.Username,
|
||||
user.Discriminator,
|
||||
user.Email,
|
||||
permissions,
|
||||
*user.Permissions,
|
||||
jwt.StandardClaims{
|
||||
ExpiresAt: time.Now().AddDate(0, 1, 0).Unix(), // one month
|
||||
Issuer: "Go Discord Bot",
|
||||
@@ -57,15 +47,6 @@ func GetJWT(user model.User) (string, error) {
|
||||
return token.SignedString([]byte(config.Config.JWTSecret))
|
||||
}
|
||||
|
||||
func checkEmailPermissions(email string, emails []string) bool {
|
||||
for _, e := range emails {
|
||||
if email == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AuthPermissions - secure end points based on auth levels
|
||||
func AuthPermissions(p int) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package model
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
@@ -20,9 +21,15 @@ type User struct {
|
||||
Verified bool `json:"verified"`
|
||||
MFAEnabled bool `json:"mfa_enabled"`
|
||||
Bot bool `json:"bot"`
|
||||
Permissions *int `gorm:"default:1;not null" json:"permissions"`
|
||||
}
|
||||
|
||||
// UserSave -
|
||||
func UserSave(conn *gorm.DB, u *User) error {
|
||||
return conn.Save(u).Error
|
||||
var userCopy User
|
||||
copier.Copy(&userCopy, u)
|
||||
// insert or update user
|
||||
// need to make copy of assign object because it must mess
|
||||
// with the actual object in FirstOrCreate method
|
||||
return conn.Where(&User{ID: u.ID}).Assign(userCopy).FirstOrCreate(u).Error
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const cashGuildID = "101198129352691712"
|
||||
|
||||
type oauthReq struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
@@ -50,6 +48,15 @@ func oauthHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// save/update user in database
|
||||
err = model.UserSave(db.GetConn(), &user)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// generate json web token
|
||||
token, err := middleware.GetJWT(user)
|
||||
|
||||
@@ -59,12 +66,5 @@ func oauthHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// save/update user in database
|
||||
err = model.UserSave(db.GetConn(), &user)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
c.JSON(200, token)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user