2022-01-21 07:23:30 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2022-01-25 05:27:40 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-01-21 07:23:30 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddUser to save user information in database
|
|
|
|
func (p *provider) AddUser(user models.User) (models.User, error) {
|
|
|
|
if user.ID == "" {
|
|
|
|
user.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Roles == "" {
|
2022-02-28 02:25:01 +00:00
|
|
|
user.Roles = strings.Join(envstore.EnvStoreObj.GetSliceStoreEnvVariable(constants.EnvKeyDefaultRoles), ",")
|
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())
|
|
|
|
_, err := userCollection.InsertOne(nil, user)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser to update user information in database
|
|
|
|
func (p *provider) UpdateUser(user models.User) (models.User, error) {
|
|
|
|
user.UpdatedAt = time.Now().Unix()
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
|
|
|
_, err := userCollection.UpdateOne(nil, bson.M{"_id": bson.M{"$eq": user.ID}}, bson.M{"$set": user}, options.MergeUpdateOptions())
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUser to delete user information from database
|
|
|
|
func (p *provider) DeleteUser(user models.User) error {
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
|
|
|
_, err := userCollection.DeleteOne(nil, bson.M{"_id": user.ID}, options.Delete())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListUsers to get list of users from database
|
2022-01-25 05:27:40 +00:00
|
|
|
func (p *provider) ListUsers(pagination model.Pagination) (*model.Users, error) {
|
|
|
|
var users []*model.User
|
|
|
|
opts := options.Find()
|
|
|
|
opts.SetLimit(pagination.Limit)
|
|
|
|
opts.SetSkip(pagination.Offset)
|
|
|
|
opts.SetSort(bson.M{"created_at": -1})
|
|
|
|
|
|
|
|
paginationClone := pagination
|
|
|
|
// TODO add pagination total
|
|
|
|
|
2022-01-21 07:23:30 +00:00
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
2022-01-25 05:27:40 +00:00
|
|
|
count, err := userCollection.CountDocuments(nil, bson.M{}, options.Count())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
paginationClone.Total = count
|
|
|
|
|
|
|
|
cursor, err := userCollection.Find(nil, 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
|
|
|
}
|
|
|
|
defer cursor.Close(nil)
|
|
|
|
|
|
|
|
for cursor.Next(nil) {
|
|
|
|
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
|
|
|
|
func (p *provider) GetUserByEmail(email string) (models.User, error) {
|
|
|
|
var user models.User
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
|
|
|
err := userCollection.FindOne(nil, bson.M{"email": email}).Decode(&user)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByID to get user information from database using user ID
|
|
|
|
func (p *provider) GetUserByID(id string) (models.User, error) {
|
|
|
|
var user models.User
|
|
|
|
|
|
|
|
userCollection := p.db.Collection(models.Collections.User, options.Collection())
|
|
|
|
err := userCollection.FindOne(nil, bson.M{"_id": id}).Decode(&user)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|