]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - api/user.go
Admin minimal dashboard.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / user.go
index a2737fd0a06c9f8e3cdacc81ef1990168982cea3..ff539f0c388d395a157e579515addf1d28fae08b 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")
 
@@ -48,7 +62,8 @@ type SignParams struct {
 }
 
 type SignResult struct {
-       Token string `json:"token"`
+       Token   string `json:"token"`
+       IsAdmin bool   `json:"isAdmin"`
 }
 
 func (s SignParams) Validate() *Error {
@@ -128,7 +143,7 @@ func (q SignupQuery) Run() (interface{}, *Error) {
                }
        }
 
-       return SignResult{token}, nil
+       return SignResult{token, newUser.Role == db.RoleAdmin}, nil
 }
 
 type SigninQuery struct {
@@ -159,7 +174,7 @@ func (q SigninQuery) Run() (interface{}, *Error) {
                return nil, NewInternalError(err)
        }
 
-       return SignResult{token}, nil
+       return SignResult{token, user.Role == db.RoleAdmin}, nil
 }
 
 type ConfirmEmailQuery struct {