mirror of
https://github.com/Oxalide/vsphere-influxdb-go.git
synced 2023-10-10 13:36:51 +02:00
30 lines
804 B
Go
30 lines
804 B
Go
package meta
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/influxdata/influxdb/influxql"
|
|
)
|
|
|
|
// WriteAuthorizer determines whether a user is authorized to write to a given database.
|
|
type WriteAuthorizer struct {
|
|
Client *Client
|
|
}
|
|
|
|
// NewWriteAuthorizer returns a new instance of WriteAuthorizer.
|
|
func NewWriteAuthorizer(c *Client) *WriteAuthorizer {
|
|
return &WriteAuthorizer{Client: c}
|
|
}
|
|
|
|
// AuthorizeWrite returns nil if the user has permission to write to the database.
|
|
func (a WriteAuthorizer) AuthorizeWrite(username, database string) error {
|
|
u, err := a.Client.User(username)
|
|
if err != nil || u == nil || !u.AuthorizeDatabase(influxql.WritePrivilege, database) {
|
|
return &ErrAuthorize{
|
|
Database: database,
|
|
Message: fmt.Sprintf("%s not authorized to write to %s", username, database),
|
|
}
|
|
}
|
|
return nil
|
|
}
|