Lots of progress on account management
This commit is contained in:
+90
-8
@@ -1,11 +1,18 @@
|
||||
package api
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func init() {
|
||||
addRoutes(func(self *HttpServer) {
|
||||
self.addRoute("POST", "/api/account/collaborators/add", self.addCollaborator)
|
||||
self.addRoute("POST", "/api/account/collaborators/remove", self.removeCollaborator)
|
||||
self.addRoute("GET", "/api/account/", self.getAccount)
|
||||
self.addRoute("GET", "/api/account/others", self.getOtherAccounts)
|
||||
self.addRoute("POST", "/api/account/using/:id", self.setUsingAccount)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -22,44 +29,119 @@ func (self *HttpServer) getAccount(c *gin.Context, auth *authContext) {
|
||||
model.Collaborators = append(model.Collaborators, &collaboratorInfoDto{
|
||||
AccountId: collaborator.AccountId,
|
||||
Role: collaborator.Role,
|
||||
Email: collaborator.Email,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(200, model)
|
||||
}
|
||||
|
||||
func (self *HttpServer) getOtherAccounts(c *gin.Context, auth *authContext) {
|
||||
var account = auth.userAccount
|
||||
|
||||
otherAccounts, err := self.store.GetOtherAccountsFor(account.Id)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var result []*otherAccountDto
|
||||
result = append(result, &otherAccountDto{
|
||||
Id: account.Id,
|
||||
Role: "owner",
|
||||
IsUsing: account.Id == account.UsingAccountId,
|
||||
Name: account.Email,
|
||||
})
|
||||
|
||||
for _, other := range otherAccounts {
|
||||
result = append(result, &otherAccountDto{
|
||||
Id: other.Id,
|
||||
Role: other.Role,
|
||||
Name: other.Name,
|
||||
IsUsing: other.Id == account.UsingAccountId,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(200, result)
|
||||
}
|
||||
|
||||
func (self *HttpServer) addCollaborator(c *gin.Context, auth *authContext) {
|
||||
var model addCollaboratorDto
|
||||
|
||||
if !c.EnsureBody(&model) {
|
||||
c.JSON(400, gin.H{"status": "Collaborator not found"})
|
||||
c.JSON(400, gin.H{"message": "Invalid request"})
|
||||
return
|
||||
}
|
||||
|
||||
collaborator, err := self.store.GetAccountByLogin(model.Email)
|
||||
if err != nil {
|
||||
c.JSON(404, gin.H{"status": "Collaborator not found"})
|
||||
c.JSON(404, gin.H{"message": "Collaborator not found"})
|
||||
return
|
||||
}
|
||||
|
||||
userAccount := auth.userAccount
|
||||
|
||||
if collaborator.Id == userAccount.Id {
|
||||
c.JSON(400, gin.H{"status": "Cannot add yourself as collaborator"})
|
||||
c.JSON(400, gin.H{"message": "Cannot add yourself as collaborator"})
|
||||
return
|
||||
}
|
||||
|
||||
err = userAccount.AddCollaborator(collaborator.Id)
|
||||
err = userAccount.AddCollaborator(collaborator)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"status": err.Error()})
|
||||
c.JSON(400, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
err = self.store.UpdateAccount(userAccount)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": err.Error()})
|
||||
c.JSON(500, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"status": "Collaborator added"})
|
||||
c.Abort(204)
|
||||
}
|
||||
|
||||
func (self *HttpServer) removeCollaborator(c *gin.Context, auth *authContext) {
|
||||
var model removeCollaboratorDto
|
||||
if !c.EnsureBody(&model) {
|
||||
c.JSON(400, gin.H{"message": "Invalid request"})
|
||||
return
|
||||
}
|
||||
|
||||
account := auth.userAccount
|
||||
account.RemoveCollaborator(model.AccountId)
|
||||
|
||||
err := self.store.UpdateAccount(account)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Abort(204)
|
||||
}
|
||||
|
||||
func (self *HttpServer) setUsingAccount(c *gin.Context, auth *authContext) {
|
||||
idString := c.Params.ByName("id")
|
||||
id, _ := strconv.Atoi(idString)
|
||||
|
||||
account := auth.userAccount
|
||||
otherAccount, err := self.store.GetAccount(id)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if otherAccount.Id != account.Id && !otherAccount.HasCollaborator(account.Id) {
|
||||
c.Abort(401)
|
||||
return
|
||||
}
|
||||
|
||||
account.UsingAccountId = otherAccount.Id
|
||||
err = self.store.UpdateAccount(account)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Abort(204)
|
||||
}
|
||||
|
||||
+3
-3
@@ -23,18 +23,18 @@ func (self *HttpServer) auth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
session, _ := sessionStore.Get(c.Request, "grafana-session")
|
||||
|
||||
if c.Request.URL.Path != "/login" && session.Values["userAccountId"] == nil {
|
||||
if c.Request.URL.Path != "/login" && session.Values["accountId"] == nil {
|
||||
self.authDenied(c)
|
||||
return
|
||||
}
|
||||
|
||||
account, err := self.store.GetAccount(session.Values["userAccountId"].(int))
|
||||
account, err := self.store.GetAccount(session.Values["accountId"].(int))
|
||||
if err != nil {
|
||||
self.authDenied(c)
|
||||
return
|
||||
}
|
||||
|
||||
usingAccount, err := self.store.GetAccount(session.Values["usingAccountId"].(int))
|
||||
usingAccount, err := self.store.GetAccount(account.UsingAccountId)
|
||||
if err != nil {
|
||||
self.authDenied(c)
|
||||
return
|
||||
|
||||
@@ -16,3 +16,14 @@ type collaboratorInfoDto struct {
|
||||
type addCollaboratorDto struct {
|
||||
Email string `json:"email" binding:"required"`
|
||||
}
|
||||
|
||||
type removeCollaboratorDto struct {
|
||||
AccountId int `json:"accountId" binding:"required"`
|
||||
}
|
||||
|
||||
type otherAccountDto struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Role string `json:"role"`
|
||||
IsUsing bool `json:"isUsing"`
|
||||
}
|
||||
|
||||
@@ -26,7 +26,8 @@ func (self *HttpServer) loginPost(c *gin.Context) {
|
||||
|
||||
account, err := self.store.GetAccountByLogin(loginModel.Email)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"status": "some error"})
|
||||
c.JSON(400, gin.H{"status": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if loginModel.Password != account.Password {
|
||||
@@ -35,8 +36,7 @@ func (self *HttpServer) loginPost(c *gin.Context) {
|
||||
}
|
||||
|
||||
session, _ := sessionStore.Get(c.Request, "grafana-session")
|
||||
session.Values["userAccountId"] = account.Id
|
||||
session.Values["usingAccountId"] = account.UsingAccountId
|
||||
session.Values["accountId"] = account.Id
|
||||
session.Save(c.Request, c.Writer)
|
||||
|
||||
var resp = &LoginResultDto{}
|
||||
|
||||
Reference in New Issue
Block a user