Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing the relations for the main model while using on other model. #1270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thienhung1989
Copy link

Fix the missing relations of the main model while containing the child model trying mapping to parent.

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

AdminUser -> TwoFaTokens -> AdminUser
AdminUser -> TwoFaDevices -> AdminUser
AdminUser -> LoginHistory -> AdminUser
AdminUser -> Roles -> Permissions
		uQ := s.app.Query.AdminUser
		uR, err = uQ.
			Preload(
				uQ.PermissionsJoin,
				uQ.Roles.Permissions,  // This fix allow calling like this.
			).
			Where(uQ.Email.Eq(email)).
			First()

User Case Description

There is missing the relations AdminUser -> Roles -> Permissions. Because, The relation: TwoFaTokens/TwoFaDevices/LoginHistory calling to AdminUser and set relations of AdminUser -> Role with is empty relations.

I'm tried with the design:

// BaseUser struct
// Define common fields for both Seeker, Giver and AdminUser
type BaseUser struct {
	ID                 uint64     `gorm:"primaryKey" json:"id"`
	Email              string     `gorm:"uniqueIndex" json:"email"`
	Password           string     `json:"-"`
	FirstName          string     `json:"firstName"`
	LastName           string     `json:"lastName"`
	PasswordResetToken *string    `json:"-"`
	ResetTokenExpire   *time.Time `json:"-"`
}

type AdminUser struct {
	BaseUser
	TwoFaTokens  *[]AdminUserTwoFaToken   `json:"-" gorm:"constraint:OnDelete:CASCADE;"`
	TwoFaDevices *[]AdminUserTwoFaDevice  `json:"-" gorm:"constraint:OnDelete:CASCADE;"`
	LoginHistory *[]AdminUserLoginHistory `json:"-" gorm:"constraint:OnDelete:CASCADE;"`

	Permissions     *[]AdminPermission     `json:"-" gorm:"many2many:AdminUserPermission;constraint:OnDelete:CASCADE;"`
	Roles           *[]AdminRole           `json:"-" gorm:"many2many:AdminUserRole;constraint:OnDelete:CASCADE;"`
}


type AdminRole struct {
	ID              uint64                 `json:"id" gorm:"primaryKey;autoIncrement"`
	Name            string                 `json:"name"`
	Permissions     *[]AdminPermission     `json:"permissions" gorm:"many2many:AdminRolePermission;constraint:OnDelete:CASCADE;"`
}

type AdminPermission struct {
	Key string `gorm:"primarykey"`
}

type AdminUserLoginHistory struct {
	ID         uint64  `json:"id" gorm:"primaryKey"`
	DeviceName *string `json:"deviceName"`
	DeviceIp   *string `json:"deviceIp"`
	Status     int8    `json:"status"`
	AdminUserID uint64     `json:"adminUserId" gorm:"index"`
	AdminUser   *AdminUser `json:"-" gorm:"foreignKey:AdminUserID;constraint:OnDelete:CASCADE"`
}

type AdminUserSocialAccount struct {
	ID           uint64     `json:"id" gorm:"primaryKey"`
	Provider     string     `json:"provider"`
	ProviderID   string     `json:"providerId"`
	AccessToken  *string    `json:"accessToken"`
	RefreshToken *string    `json:"refreshToken"`
	AdminUserID uint64     `json:"adminUserId" gorm:"index"`
	AdminUser   *AdminUser `json:"-" gorm:"foreignKey:AdminUserID;constraint:OnDelete:CASCADE"`
}

type AdminUserTwoFaToken struct {
	ID          uint64  `json:"id" gorm:"primaryKey"`
	OtpType     uint    `json:"otpType"`
	Secret      string  `json:"-"`
	Extra       *string `json:"extra"`
	BackupCodes *string `json:"-"`
	AdminUserID uint64     `json:"adminUserId" gorm:"index"`
	AdminUser   *AdminUser `json:"-" gorm:"foreignKey:AdminUserID;constraint:OnDelete:CASCADE"`
}

type AdminUserTwoFaDevice struct {
	ID          uint64 `json:"id" gorm:"primaryKey"`
	DeviceName  string `json:"deviceName"`
	DeviceToken string `json:"-"`
	AdminUserID uint64     `json:"adminUserId" gorm:"index"`
	AdminUser   *AdminUser `json:"-" gorm:"foreignKey:AdminUserID;constraint:OnDelete:CASCADE"`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant