mirror of
				https://github.com/grafana/grafana.git
				synced 2025-02-25 18:55:37 -06:00 
			
		
		
		
	Big refactoring for context.User, and how current user info is fetching, now included collaborator role
This commit is contained in:
		@@ -13,8 +13,8 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type AuthOptions struct {
 | 
			
		||||
	ReqAdmin    bool
 | 
			
		||||
	ReqSignedIn bool
 | 
			
		||||
	ReqGrafanaAdmin bool
 | 
			
		||||
	ReqSignedIn     bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getRequestAccountId(c *Context) (int64, error) {
 | 
			
		||||
@@ -68,7 +68,7 @@ func Auth(options *AuthOptions) macaron.Handler {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if !c.IsAdmin && options.ReqAdmin {
 | 
			
		||||
		if !c.IsGrafanaAdmin && options.ReqGrafanaAdmin {
 | 
			
		||||
			authDenied(c)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,17 +16,11 @@ import (
 | 
			
		||||
 | 
			
		||||
type Context struct {
 | 
			
		||||
	*macaron.Context
 | 
			
		||||
	*m.SignInUser
 | 
			
		||||
 | 
			
		||||
	Session session.Store
 | 
			
		||||
 | 
			
		||||
	IsSignedIn bool
 | 
			
		||||
	IsAdmin    bool
 | 
			
		||||
 | 
			
		||||
	Account     *m.Account
 | 
			
		||||
	UserAccount *m.Account
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Context) GetAccountId() int64 {
 | 
			
		||||
	return c.Account.Id
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetContextHandler() macaron.Handler {
 | 
			
		||||
@@ -38,27 +32,15 @@ func GetContextHandler() macaron.Handler {
 | 
			
		||||
 | 
			
		||||
		// try get account id from request
 | 
			
		||||
		if accountId, err := getRequestAccountId(ctx); err == nil {
 | 
			
		||||
			// fetch user
 | 
			
		||||
			userQuery := m.GetAccountByIdQuery{Id: accountId}
 | 
			
		||||
			if err := bus.Dispatch(&userQuery); err != nil {
 | 
			
		||||
			query := m.GetSignedInUserQuery{AccountId: accountId}
 | 
			
		||||
			if err := bus.Dispatch(&query); err != nil {
 | 
			
		||||
				log.Error(3, "Failed to get user by id, %v, %v", accountId, err)
 | 
			
		||||
			} else {
 | 
			
		||||
				// fetch using account
 | 
			
		||||
				ctx.UserAccount = userQuery.Result
 | 
			
		||||
				usingQuery := m.GetAccountByIdQuery{Id: ctx.UserAccount.UsingAccountId}
 | 
			
		||||
				if err := bus.Dispatch(&usingQuery); err != nil {
 | 
			
		||||
					log.Error(3, "Faild to get account's using account, account: %v, usingAccountId: %v, err:%v", accountId, ctx.UserAccount.Id, err)
 | 
			
		||||
				} else {
 | 
			
		||||
					ctx.Account = usingQuery.Result
 | 
			
		||||
				}
 | 
			
		||||
				ctx.IsSignedIn = true
 | 
			
		||||
				ctx.SignInUser = query.Result
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if ctx.Account != nil {
 | 
			
		||||
			ctx.IsSignedIn = true
 | 
			
		||||
			ctx.IsAdmin = ctx.Account.IsAdmin
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		c.Map(ctx)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user