2021-07-12 18:22:16 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2021-12-17 15:55:07 +00:00
|
|
|
"fmt"
|
2021-07-12 18:22:16 +00:00
|
|
|
"log"
|
2021-09-21 02:53:40 +00:00
|
|
|
"time"
|
2021-07-12 18:22:16 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
"github.com/arangodb/go-driver"
|
|
|
|
arangoDriver "github.com/arangodb/go-driver"
|
2021-12-23 05:01:52 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-10-03 16:03:55 +00:00
|
|
|
"github.com/google/uuid"
|
2021-12-20 17:51:27 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2021-07-12 18:22:16 +00:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
2021-12-22 05:21:12 +00:00
|
|
|
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
|
|
|
|
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
|
|
|
|
2021-12-22 10:01:45 +00:00
|
|
|
Email string `gorm:"unique" json:"email" bson:"email"`
|
2021-12-22 10:08:51 +00:00
|
|
|
EmailVerifiedAt *int64 `json:"email_verified_at" bson:"email_verified_at"`
|
2021-12-22 10:01:45 +00:00
|
|
|
Password *string `gorm:"type:text" json:"password" bson:"password"`
|
|
|
|
SignupMethods string `json:"signup_methods" bson:"signup_methods"`
|
|
|
|
GivenName *string `json:"given_name" bson:"given_name"`
|
|
|
|
FamilyName *string `json:"family_name" bson:"family_name"`
|
|
|
|
MiddleName *string `json:"middle_name" bson:"middle_name"`
|
|
|
|
Nickname *string `json:"nickname" bson:"nickname"`
|
|
|
|
Gender *string `json:"gender" bson:"gender"`
|
|
|
|
Birthdate *string `json:"birthdate" bson:"birthdate"`
|
|
|
|
PhoneNumber *string `gorm:"unique" json:"phone_number" bson:"phone_number"`
|
|
|
|
PhoneNumberVerifiedAt *int64 `json:"phone_number_verified_at" bson:"phone_number_verified_at"`
|
|
|
|
Picture *string `gorm:"type:text" json:"picture" bson:"picture"`
|
|
|
|
Roles string `json:"roles" bson:"roles"`
|
|
|
|
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
|
|
|
|
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
// AddUser function to add user even with email conflict
|
|
|
|
func (mgr *manager) AddUser(user User) (User, error) {
|
|
|
|
if user.ID == "" {
|
|
|
|
user.ID = uuid.New().String()
|
|
|
|
}
|
2021-10-03 16:03:55 +00:00
|
|
|
|
2021-12-23 05:01:52 +00:00
|
|
|
if user.Roles == "" {
|
|
|
|
user.Roles = constants.DEFAULT_ROLES[0]
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
// copy id as value for fields required for mongodb & arangodb
|
|
|
|
user.Key = user.ID
|
|
|
|
result := mgr.sqlDB.Clauses(
|
|
|
|
clause.OnConflict{
|
|
|
|
UpdateAll: true,
|
|
|
|
Columns: []clause.Column{{Name: "email"}},
|
|
|
|
}).Create(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error adding user:", result.Error)
|
|
|
|
return user, result.Error
|
|
|
|
}
|
|
|
|
}
|
2021-10-03 16:03:55 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsArangoDB {
|
|
|
|
user.CreatedAt = time.Now().Unix()
|
|
|
|
user.UpdatedAt = time.Now().Unix()
|
|
|
|
userCollection, _ := mgr.arangodb.Collection(nil, Collections.User)
|
2021-12-20 17:51:27 +00:00
|
|
|
meta, err := userCollection.CreateDocument(arangoDriver.WithOverwrite(nil), user)
|
2021-12-17 15:55:07 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error adding user:", err)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
user.Key = meta.Key
|
2021-12-22 10:01:45 +00:00
|
|
|
user.ID = meta.ID.String()
|
2021-07-18 03:55:20 +00:00
|
|
|
}
|
2021-12-20 17:51:27 +00:00
|
|
|
|
|
|
|
if IsMongoDB {
|
|
|
|
user.CreatedAt = time.Now().Unix()
|
|
|
|
user.UpdatedAt = time.Now().Unix()
|
|
|
|
user.Key = user.ID
|
|
|
|
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
|
|
|
|
_, err := userCollection.InsertOne(nil, user)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error adding user:", err)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 03:55:20 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser function to update user with ID conflict
|
|
|
|
func (mgr *manager) UpdateUser(user User) (User, error) {
|
2021-09-21 02:53:40 +00:00
|
|
|
user.UpdatedAt = time.Now().Unix()
|
2021-12-17 15:55:07 +00:00
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
result := mgr.sqlDB.Save(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error updating user:", result.Error)
|
|
|
|
return user, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
collection, _ := mgr.arangodb.Collection(nil, Collections.User)
|
|
|
|
meta, err := collection.UpdateDocument(nil, user.Key, user)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error updating user:", err)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Key = meta.Key
|
2021-12-22 10:01:45 +00:00
|
|
|
user.ID = meta.ID.String()
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
2021-12-20 17:51:27 +00:00
|
|
|
|
|
|
|
if IsMongoDB {
|
|
|
|
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
|
2021-12-23 05:01:52 +00:00
|
|
|
_, err := userCollection.UpdateOne(nil, bson.M{"_id": bson.M{"$eq": user.ID}}, bson.M{"$set": user}, options.MergeUpdateOptions())
|
2021-12-20 17:51:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error updating user:", err)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:22:16 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsers function to get all users
|
|
|
|
func (mgr *manager) GetUsers() ([]User, error) {
|
|
|
|
var users []User
|
2021-12-17 15:55:07 +00:00
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
result := mgr.sqlDB.Find(&users)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error getting users:", result.Error)
|
|
|
|
return users, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s RETURN d", Collections.User)
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var user User
|
|
|
|
meta, err := cursor.ReadDocument(nil, &user)
|
|
|
|
|
|
|
|
if driver.IsNoMoreDocuments(err) {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Key != "" {
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
2021-12-20 17:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsMongoDB {
|
|
|
|
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
|
|
|
|
cursor, err := userCollection.Find(nil, bson.M{}, options.Find())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error getting users:", err)
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
defer cursor.Close(nil)
|
2021-12-17 15:55:07 +00:00
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
for cursor.Next(nil) {
|
|
|
|
var user User
|
|
|
|
err := cursor.Decode(&user)
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
users = append(users, user)
|
2021-12-17 15:55:07 +00:00
|
|
|
}
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
2021-12-20 17:51:27 +00:00
|
|
|
|
2021-07-12 18:22:16 +00:00
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *manager) GetUserByEmail(email string) (User, error) {
|
|
|
|
var user User
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
result := mgr.sqlDB.Where("email = ?", email).First(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return user, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
2021-12-20 12:03:11 +00:00
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d.email == @email RETURN d", Collections.User)
|
2021-12-17 15:55:07 +00:00
|
|
|
bindVars := map[string]interface{}{
|
|
|
|
"email": email,
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, bindVars)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
2021-12-20 12:03:11 +00:00
|
|
|
if !cursor.HasMore() {
|
|
|
|
if user.Key == "" {
|
|
|
|
return user, fmt.Errorf("user not found")
|
|
|
|
}
|
2021-12-17 15:55:07 +00:00
|
|
|
break
|
2021-12-20 12:03:11 +00:00
|
|
|
}
|
|
|
|
_, err := cursor.ReadDocument(nil, &user)
|
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsMongoDB {
|
|
|
|
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
|
|
|
|
err := userCollection.FindOne(nil, bson.M{"email": email}).Decode(&user)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:22:16 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-09-21 02:53:40 +00:00
|
|
|
func (mgr *manager) GetUserByID(id string) (User, error) {
|
|
|
|
var user User
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
result := mgr.sqlDB.Where("id = ?", id).First(&user)
|
2021-09-21 02:53:40 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
return user, result.Error
|
|
|
|
}
|
2021-07-15 12:02:55 +00:00
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsArangoDB {
|
2021-12-23 05:01:52 +00:00
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d._id == @id LIMIT 1 RETURN d", Collections.User)
|
2021-12-17 15:55:07 +00:00
|
|
|
bindVars := map[string]interface{}{
|
|
|
|
"id": id,
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, bindVars)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
2021-12-20 12:03:11 +00:00
|
|
|
if !cursor.HasMore() {
|
|
|
|
if user.Key == "" {
|
|
|
|
return user, fmt.Errorf("user not found")
|
|
|
|
}
|
2021-12-17 15:55:07 +00:00
|
|
|
break
|
2021-12-20 12:03:11 +00:00
|
|
|
}
|
|
|
|
_, err := cursor.ReadDocument(nil, &user)
|
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsMongoDB {
|
|
|
|
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
|
2021-12-23 05:01:52 +00:00
|
|
|
err := userCollection.FindOne(nil, bson.M{"_id": id}).Decode(&user)
|
2021-12-20 17:51:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
return user, nil
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
2021-08-06 13:47:52 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
func (mgr *manager) DeleteUser(user User) error {
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
result := mgr.sqlDB.Delete(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error deleting user:`, result.Error)
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 13:47:52 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsArangoDB {
|
|
|
|
collection, _ := mgr.arangodb.Collection(nil, Collections.User)
|
|
|
|
_, err := collection.RemoveDocument(nil, user.Key)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`error deleting user:`, err)
|
|
|
|
return err
|
|
|
|
}
|
2021-08-06 13:47:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsMongoDB {
|
|
|
|
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
|
2021-12-23 05:01:52 +00:00
|
|
|
_, err := userCollection.DeleteOne(nil, bson.M{"_id": user.ID}, options.Delete())
|
2021-12-20 17:51:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error deleting user:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:47:52 +00:00
|
|
|
return nil
|
|
|
|
}
|