2022-01-21 07:23:30 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2022-07-10 16:19:33 +00:00
|
|
|
"context"
|
2022-01-21 07:23:30 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2022-01-25 05:27:40 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-05-29 11:52:46 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-01-21 07:23:30 +00:00
|
|
|
"github.com/google/uuid"
|
2022-08-02 08:42:36 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-21 07:23:30 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2022-08-02 08:42:36 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2022-01-21 07:23:30 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddUser to save user information in database
|
2022-07-10 16:19:33 +00:00
|
|
|
func (p *provider) AddUser(ctx context.Context, user models.User) (models.User, error) {
|
2022-01-21 07:23:30 +00:00
|
|
|
if user.ID == "" {
|
|
|
|
user.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Roles == "" {
|
2022-05-31 02:44:03 +00:00
|
|
|
defaultRoles, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyDefaultRoles)
|
2022-05-29 11:52:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2022-05-31 02:44:03 +00:00
|
|
|
user.Roles = defaultRoles
|
2022-01-21 07:23:30 +00:00
|
|
|
}
|
|
|
|
user.CreatedAt = time.Now().Unix()
|
|
|
|
user.UpdatedAt = time.Now().Unix()
|
|
|
|
user.Key = user.ID
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
2022-07-10 16:19:33 +00:00
|
|
|
_, err := userCollection.InsertOne(ctx, user)
|
2022-01-21 07:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser to update user information in database
|
2022-07-10 16:19:33 +00:00
|
|
|
func (p *provider) UpdateUser(ctx context.Context, user models.User) (models.User, error) {
|
2022-01-21 07:23:30 +00:00
|
|
|
user.UpdatedAt = time.Now().Unix()
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
2022-07-10 16:19:33 +00:00
|
|
|
_, err := userCollection.UpdateOne(ctx, bson.M{"_id": bson.M{"$eq": user.ID}}, bson.M{"$set": user}, options.MergeUpdateOptions())
|
2022-01-21 07:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUser to delete user information from database
|
2022-07-10 16:19:33 +00:00
|
|
|
func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
|
2022-01-21 07:23:30 +00:00
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
2022-07-10 16:19:33 +00:00
|
|
|
_, err := userCollection.DeleteOne(ctx, bson.M{"_id": user.ID}, options.Delete())
|
2022-01-21 07:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-12 03:12:32 +00:00
|
|
|
sessionCollection := p.db.Collection(models.Collections.Session, options.Collection())
|
|
|
|
_, err = sessionCollection.DeleteMany(ctx, bson.M{"user_id": user.ID}, options.Delete())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-21 07:23:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListUsers to get list of users from database
|
2022-07-10 16:19:33 +00:00
|
|
|
func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (*model.Users, error) {
|
2022-01-25 05:27:40 +00:00
|
|
|
var users []*model.User
|
|
|
|
opts := options.Find()
|
|
|
|
opts.SetLimit(pagination.Limit)
|
|
|
|
opts.SetSkip(pagination.Offset)
|
|
|
|
opts.SetSort(bson.M{"created_at": -1})
|
|
|
|
|
|
|
|
paginationClone := pagination
|
|
|
|
|
2022-01-21 07:23:30 +00:00
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
2022-07-10 16:19:33 +00:00
|
|
|
count, err := userCollection.CountDocuments(ctx, bson.M{}, options.Count())
|
2022-01-25 05:27:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
paginationClone.Total = count
|
|
|
|
|
2022-07-10 16:19:33 +00:00
|
|
|
cursor, err := userCollection.Find(ctx, bson.M{}, opts)
|
2022-01-21 07:23:30 +00:00
|
|
|
if err != nil {
|
2022-01-25 05:27:40 +00:00
|
|
|
return nil, err
|
2022-01-21 07:23:30 +00:00
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
defer cursor.Close(ctx)
|
2022-01-21 07:23:30 +00:00
|
|
|
|
2022-07-10 16:19:33 +00:00
|
|
|
for cursor.Next(ctx) {
|
2022-01-21 07:23:30 +00:00
|
|
|
var user models.User
|
|
|
|
err := cursor.Decode(&user)
|
|
|
|
if err != nil {
|
2022-01-25 05:27:40 +00:00
|
|
|
return nil, err
|
2022-01-21 07:23:30 +00:00
|
|
|
}
|
2022-01-25 05:27:40 +00:00
|
|
|
users = append(users, user.AsAPIUser())
|
2022-01-21 07:23:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 05:27:40 +00:00
|
|
|
return &model.Users{
|
|
|
|
Pagination: &paginationClone,
|
|
|
|
Users: users,
|
|
|
|
}, nil
|
2022-01-21 07:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByEmail to get user information from database using email address
|
2022-07-10 16:19:33 +00:00
|
|
|
func (p *provider) GetUserByEmail(ctx context.Context, email string) (models.User, error) {
|
2022-01-21 07:23:30 +00:00
|
|
|
var user models.User
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
2022-07-10 16:19:33 +00:00
|
|
|
err := userCollection.FindOne(ctx, bson.M{"email": email}).Decode(&user)
|
2022-01-21 07:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByID to get user information from database using user ID
|
2022-07-10 16:19:33 +00:00
|
|
|
func (p *provider) GetUserByID(ctx context.Context, id string) (models.User, error) {
|
2022-01-21 07:23:30 +00:00
|
|
|
var user models.User
|
|
|
|
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
2022-07-10 16:19:33 +00:00
|
|
|
err := userCollection.FindOne(ctx, bson.M{"_id": id}).Decode(&user)
|
2022-01-21 07:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
2022-08-02 08:42:36 +00:00
|
|
|
|
|
|
|
// UpdateUsers to update multiple users, with parameters of user IDs slice
|
|
|
|
// If ids set to nil / empty all the users will be updated
|
|
|
|
func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{}, ids []string) error {
|
|
|
|
// set updated_at time for all users
|
|
|
|
data["updated_at"] = time.Now().Unix()
|
|
|
|
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
|
|
|
|
|
|
|
var res *mongo.UpdateResult
|
|
|
|
var err error
|
|
|
|
if ids != nil && len(ids) > 0 {
|
|
|
|
res, err = userCollection.UpdateMany(ctx, bson.M{"_id": bson.M{"$in": ids}}, bson.M{"$set": data})
|
|
|
|
} else {
|
|
|
|
res, err = userCollection.UpdateMany(ctx, bson.M{}, bson.M{"$set": data})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
log.Info("Updated users: ", res.ModifiedCount)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|