[WIP]
This commit is contained in:
@@ -18,12 +18,12 @@ type ServerPermissions struct {
|
||||
Permissions models.Permission `bson:"permissions"`
|
||||
}
|
||||
|
||||
type AuthorizationDbHandler struct {
|
||||
type ServersAuthorizationDbHandler struct {
|
||||
dbhandler.ServersAuthorizationDbHandler
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
func (self *AuthorizationDbHandler) RemoveUser(ctx context.Context, username string) error {
|
||||
func (self *ServersAuthorizationDbHandler) RemoveUser(ctx context.Context, username string) error {
|
||||
_, err := self.collection.DeleteMany(
|
||||
ctx,
|
||||
bson.M{
|
||||
@@ -34,7 +34,7 @@ func (self *AuthorizationDbHandler) RemoveUser(ctx context.Context, username str
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *AuthorizationDbHandler) RemoveServer(ctx context.Context, serverId string) error {
|
||||
func (self *ServersAuthorizationDbHandler) RemoveServer(ctx context.Context, serverId string) error {
|
||||
_, err := self.collection.DeleteMany(
|
||||
ctx,
|
||||
bson.M{
|
||||
@@ -45,7 +45,7 @@ func (self *AuthorizationDbHandler) RemoveServer(ctx context.Context, serverId s
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *AuthorizationDbHandler) AddPermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
func (self *ServersAuthorizationDbHandler) AddPermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
var serverPermissions ServerPermissions
|
||||
err := self.collection.FindOne(
|
||||
ctx,
|
||||
@@ -76,7 +76,7 @@ func (self *AuthorizationDbHandler) AddPermissions(ctx context.Context, username
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *AuthorizationDbHandler) RemovePermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
func (self *ServersAuthorizationDbHandler) RemovePermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
var serverPermissions ServerPermissions
|
||||
err := self.collection.FindOne(
|
||||
ctx,
|
||||
@@ -107,7 +107,7 @@ func (self *AuthorizationDbHandler) RemovePermissions(ctx context.Context, usern
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *AuthorizationDbHandler) SetPermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
func (self *ServersAuthorizationDbHandler) SetPermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
_, err := self.collection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{
|
||||
@@ -123,7 +123,7 @@ func (self *AuthorizationDbHandler) SetPermissions(ctx context.Context, username
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *AuthorizationDbHandler) GetPermissions(ctx context.Context, username string, serverId string) (models.Permission, error) {
|
||||
func (self *ServersAuthorizationDbHandler) GetPermissions(ctx context.Context, username string, serverId string) (models.Permission, error) {
|
||||
var serverPermissions ServerPermissions
|
||||
err := self.collection.FindOne(
|
||||
ctx,
|
||||
@@ -140,7 +140,7 @@ func (self *AuthorizationDbHandler) GetPermissions(ctx context.Context, username
|
||||
return serverPermissions.Permissions, nil
|
||||
}
|
||||
|
||||
func NewAuthorizationHandler(config models.MongoDBConfig) (*AuthorizationDbHandler, error) {
|
||||
func NewServersAuthorizationHandler(config models.MongoDBConfig) (*ServersAuthorizationDbHandler, error) {
|
||||
clientOptions := options.Client().ApplyURI(config.Url).SetAuth(options.Credential{
|
||||
Username: config.Username,
|
||||
Password: config.Password,
|
||||
@@ -155,7 +155,7 @@ func NewAuthorizationHandler(config models.MongoDBConfig) (*AuthorizationDbHandl
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &AuthorizationDbHandler{
|
||||
return &ServersAuthorizationDbHandler{
|
||||
collection: client.Database(config.Database).Collection(config.Collection),
|
||||
}, nil
|
||||
}
|
||||
|
@@ -41,34 +41,42 @@ func (self *UserPassAuthenticationDbHandler) ListUsers(ctx context.Context) ([]m
|
||||
modelUsers := make([]models.User, len(authUsers))
|
||||
for i, authUser := range authUsers {
|
||||
modelUsers[i] = models.User{
|
||||
Username: authUser.Username,
|
||||
Nickname: authUser.Nickname,
|
||||
Email: authUser.Email,
|
||||
Username: authUser.Username,
|
||||
Nickname: authUser.Nickname,
|
||||
Email: authUser.Email,
|
||||
MaxOwnedServers: authUser.MaxOwnedSevers,
|
||||
Permissions: authUser.Permissions,
|
||||
}
|
||||
}
|
||||
|
||||
return modelUsers, nil
|
||||
}
|
||||
|
||||
func (self *UserPassAuthenticationDbHandler) AuthenticateUser(ctx context.Context, username string, password string) (models.Permission, error) {
|
||||
func (self *UserPassAuthenticationDbHandler) AuthenticateUser(ctx context.Context, username string, password string) (*models.User, error) {
|
||||
var user AuthUser
|
||||
err := self.collection.FindOne(ctx, bson.M{"username": username}).Decode(&user)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hashedPassword, err := dbhandler.HashPassword(password)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if user.HashedPassword != hashedPassword {
|
||||
return 0, fmt.Errorf("Incorrect Password")
|
||||
return nil, fmt.Errorf("Incorrect Password")
|
||||
}
|
||||
|
||||
return user.Permissions, nil
|
||||
return &models.User{
|
||||
Username: user.Username,
|
||||
Nickname: user.Nickname,
|
||||
Email: user.Email,
|
||||
MaxOwnedServers: user.MaxOwnedSevers,
|
||||
Permissions: user.Permissions,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (self *UserPassAuthenticationDbHandler) CreateUser(
|
||||
|
Reference in New Issue
Block a user