]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/commitdiff
User roles. v0.0.13
authorjloup <jloup@jloup.work>
Sun, 13 May 2018 14:47:59 +0000 (15:47 +0100)
committerjloup <jloup@jloup.work>
Sun, 13 May 2018 14:47:59 +0000 (15:47 +0100)
api/routes.go
api/user.go
db/migrations.go
db/user.go

index 404f8214bd598e4747a953ed16086b6fa519d7b2..3adbfe95b1a9655a5fa7dc3d3a13b75019128b4b 100644 (file)
@@ -55,6 +55,11 @@ var Groups = []Group{
                        {"GET", []gin.HandlerFunc{UserAccount}, "/account"},
                },
        },
+       {
+               "/admin",
+               []Middleware{JwtAuth, UserConfirmed, UserIsAdmin, OtpAuth},
+               []Route{},
+       },
 }
 
 func Signup(c *gin.Context) {
index a2737fd0a06c9f8e3cdacc81ef1990168982cea3..bc24bbb23111d478adb6bb82636cd7bc82dc4e81 100644 (file)
@@ -30,6 +30,20 @@ func UserConfirmed(c *gin.Context) *Error {
        return nil
 }
 
+func UserIsAdmin(c *gin.Context) *Error {
+       user, exists := c.Get("user")
+
+       if !exists {
+               return &Error{NotAuthorized, "not authorized", fmt.Errorf("no user key in context")}
+       }
+
+       if user.(db.User).Role != db.RoleAdmin {
+               return &Error{NotAuthorized, "not authorized", fmt.Errorf("user '%v' is not admin", user)}
+       }
+
+       return nil
+}
+
 func GetUser(c *gin.Context) db.User {
        user, _ := c.Get("user")
 
index f0df49c6b9c4339965f783abd24c6111f85e7164..e8fc40d12762934caa1222995879a0ab2f973b3c 100644 (file)
@@ -89,4 +89,15 @@ var migrations []Migration = []Migration{
                        "DROP TYPE market_config_status",
                },
        },
+       {
+               Version: 201805131000,
+               Up: []string{
+                       "CREATE TYPE user_role AS ENUM ('admin', 'user')",
+                       "ALTER TABLE users ADD role user_role NOT NULL DEFAULT 'user'",
+               },
+               Down: []string{
+                       "ALTER TABLE users DROP COLUMN role",
+                       "DROP TYPE user_role",
+               },
+       },
 }
index 64ca6a611ee7609ae0607995c4820598372739f2..24ce491e80ab9e4e3b7ee9135912aa4da1d9c853 100644 (file)
@@ -11,10 +11,16 @@ const (
        AwaitingConfirmation
 )
 
+type UserRole string
+
+const RoleUser UserRole = "user"
+const RoleAdmin UserRole = "admin"
+
 type User struct {
        Id           int64
-       Email        string `sql:",unique,notnull"`
-       PasswordHash string `sql:",notnull"`
+       Role         UserRole
+       Email        string
+       PasswordHash string
        OtpSecret    string
        IsOtpSetup   bool
        Status       UserStatus