2022-01-21 06:48:07 +00:00
|
|
|
package models
|
|
|
|
|
2022-01-22 19:54:41 +00:00
|
|
|
import (
|
2022-08-08 20:13:37 +00:00
|
|
|
"encoding/json"
|
2022-01-22 19:54:41 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-07-15 16:41:08 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
2022-01-22 19:54:41 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 07:06:22 +00:00
|
|
|
// Note: any change here should be reflected in providers/casandra/provider.go as it does not have model support in collection creation
|
|
|
|
|
2022-01-21 06:48:07 +00:00
|
|
|
// User model for db
|
|
|
|
type User struct {
|
2022-10-02 19:38:12 +00:00
|
|
|
Key string `json:"_key,omitempty" bson:"_key,omitempty" cql:"_key,omitempty" dynamo:"key,omitempty"` // for arangodb
|
|
|
|
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id" cql:"id" dynamo:"id,hash"`
|
2022-01-21 06:48:07 +00:00
|
|
|
|
2024-03-28 08:27:47 +00:00
|
|
|
Email *string `gorm:"index" json:"email" bson:"email" cql:"email" dynamo:"email" index:"email,hash"`
|
2022-10-02 19:38:12 +00:00
|
|
|
EmailVerifiedAt *int64 `json:"email_verified_at" bson:"email_verified_at" cql:"email_verified_at" dynamo:"email_verified_at"`
|
2022-10-08 10:37:07 +00:00
|
|
|
Password *string `json:"password" bson:"password" cql:"password" dynamo:"password"`
|
2022-10-02 19:38:12 +00:00
|
|
|
SignupMethods string `json:"signup_methods" bson:"signup_methods" cql:"signup_methods" dynamo:"signup_methods"`
|
|
|
|
GivenName *string `json:"given_name" bson:"given_name" cql:"given_name" dynamo:"given_name"`
|
|
|
|
FamilyName *string `json:"family_name" bson:"family_name" cql:"family_name" dynamo:"family_name"`
|
|
|
|
MiddleName *string `json:"middle_name" bson:"middle_name" cql:"middle_name" dynamo:"middle_name"`
|
|
|
|
Nickname *string `json:"nickname" bson:"nickname" cql:"nickname" dynamo:"nickname"`
|
|
|
|
Gender *string `json:"gender" bson:"gender" cql:"gender" dynamo:"gender"`
|
|
|
|
Birthdate *string `json:"birthdate" bson:"birthdate" cql:"birthdate" dynamo:"birthdate"`
|
2022-11-17 17:38:17 +00:00
|
|
|
PhoneNumber *string `gorm:"index" json:"phone_number" bson:"phone_number" cql:"phone_number" dynamo:"phone_number"`
|
2022-10-02 19:38:12 +00:00
|
|
|
PhoneNumberVerifiedAt *int64 `json:"phone_number_verified_at" bson:"phone_number_verified_at" cql:"phone_number_verified_at" dynamo:"phone_number_verified_at"`
|
2022-10-08 10:37:07 +00:00
|
|
|
Picture *string `json:"picture" bson:"picture" cql:"picture" dynamo:"picture"`
|
2022-10-02 19:38:12 +00:00
|
|
|
Roles string `json:"roles" bson:"roles" cql:"roles" dynamo:"roles"`
|
|
|
|
RevokedTimestamp *int64 `json:"revoked_timestamp" bson:"revoked_timestamp" cql:"revoked_timestamp" dynamo:"revoked_timestamp"`
|
|
|
|
IsMultiFactorAuthEnabled *bool `json:"is_multi_factor_auth_enabled" bson:"is_multi_factor_auth_enabled" cql:"is_multi_factor_auth_enabled" dynamo:"is_multi_factor_auth_enabled"`
|
|
|
|
UpdatedAt int64 `json:"updated_at" bson:"updated_at" cql:"updated_at" dynamo:"updated_at"`
|
|
|
|
CreatedAt int64 `json:"created_at" bson:"created_at" cql:"created_at" dynamo:"created_at"`
|
2023-08-14 06:31:37 +00:00
|
|
|
AppData *string `json:"app_data" bson:"app_data" cql:"app_data" dynamo:"app_data"`
|
2022-01-21 06:48:07 +00:00
|
|
|
}
|
2022-01-22 19:54:41 +00:00
|
|
|
|
|
|
|
func (user *User) AsAPIUser() *model.User {
|
|
|
|
isEmailVerified := user.EmailVerifiedAt != nil
|
|
|
|
isPhoneVerified := user.PhoneNumberVerifiedAt != nil
|
2023-08-14 06:31:37 +00:00
|
|
|
appDataMap := make(map[string]interface{})
|
|
|
|
json.Unmarshal([]byte(refs.StringValue(user.AppData)), &appDataMap)
|
2022-08-02 08:42:36 +00:00
|
|
|
// id := user.ID
|
|
|
|
// if strings.Contains(id, Collections.User+"/") {
|
|
|
|
// id = strings.TrimPrefix(id, Collections.User+"/")
|
|
|
|
// }
|
2022-01-22 19:54:41 +00:00
|
|
|
return &model.User{
|
2022-08-02 08:42:36 +00:00
|
|
|
ID: user.ID,
|
2022-07-23 09:56:44 +00:00
|
|
|
Email: user.Email,
|
|
|
|
EmailVerified: isEmailVerified,
|
|
|
|
SignupMethods: user.SignupMethods,
|
|
|
|
GivenName: user.GivenName,
|
|
|
|
FamilyName: user.FamilyName,
|
|
|
|
MiddleName: user.MiddleName,
|
|
|
|
Nickname: user.Nickname,
|
2023-10-25 19:25:10 +00:00
|
|
|
PreferredUsername: user.Email,
|
2022-07-23 09:56:44 +00:00
|
|
|
Gender: user.Gender,
|
|
|
|
Birthdate: user.Birthdate,
|
|
|
|
PhoneNumber: user.PhoneNumber,
|
|
|
|
PhoneNumberVerified: &isPhoneVerified,
|
|
|
|
Picture: user.Picture,
|
|
|
|
Roles: strings.Split(user.Roles, ","),
|
|
|
|
RevokedTimestamp: user.RevokedTimestamp,
|
|
|
|
IsMultiFactorAuthEnabled: user.IsMultiFactorAuthEnabled,
|
|
|
|
CreatedAt: refs.NewInt64Ref(user.CreatedAt),
|
|
|
|
UpdatedAt: refs.NewInt64Ref(user.UpdatedAt),
|
2023-08-14 06:31:37 +00:00
|
|
|
AppData: appDataMap,
|
2022-01-22 19:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-08 20:13:37 +00:00
|
|
|
|
|
|
|
func (user *User) ToMap() map[string]interface{} {
|
|
|
|
res := map[string]interface{}{}
|
|
|
|
data, _ := json.Marshal(user) // Convert to a json string
|
|
|
|
json.Unmarshal(data, &res) // Convert to a map
|
|
|
|
return res
|
|
|
|
}
|