You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2021/08/20 16:00:21 UTC

[GitHub] [trafficcontrol] srijeet0406 commented on a change in pull request #6124: Role and User struct changes for permission based roles

srijeet0406 commented on a change in pull request #6124:
URL: https://github.com/apache/trafficcontrol/pull/6124#discussion_r693055877



##########
File path: lib/go-tc/users.go
##########
@@ -33,6 +33,249 @@ import (
 	"github.com/go-ozzo/ozzo-validation/is"
 )
 
+// A UserV50 is a representation of a Traffic Ops user as it appears in version
+// 5.0 of Traffic Ops's API.
+type UserV50 struct {
+	AddressLine1         *string    `json:"addressLine1" db:"address_line1"`
+	AddressLine2         *string    `json:"addressLine2" db:"address_line2"`
+	ChangeLogCount       *int       `json:"changeLogCount" db:"change_log_count"`
+	City                 *string    `json:"city" db:"city"`
+	Company              *string    `json:"company" db:"company"`
+	ConfirmLocalPassword *string    `json:"confirmLocalPasswd,omitempty" db:"confirm_local_passwd"`
+	Country              *string    `json:"country" db:"country"`
+	Email                *string    `json:"email" db:"email"`
+	FullName             *string    `json:"fullName" db:"full_name"`
+	GID                  *int       `json:"gid"`
+	ID                   *int       `json:"id" db:"id"`
+	LastAuthenticated    time.Time  `json:"lastAuthenticated" db:"last_authenticated"`
+	LastUpdated          time.Time  `json:"lastUpdated" db:"last_updated"`
+	LocalPassword        *string    `json:"localPasswd,omitempty" db:"local_passwd"`
+	NewUser              bool       `json:"newUser" db:"new_user"`
+	PhoneNumber          *string    `json:"phoneNumber" db:"phone_number"`
+	PostalCode           *string    `json:"postalCode" db:"postal_code"`
+	PublicSSHKey         *string    `json:"publicSshKey" db:"public_ssh_key"`
+	RegistrationSent     *time.Time `json:"registrationSent" db:"registration_sent"`
+	Role                 string     `json:"role" db:"role"`
+	StateOrProvince      *string    `json:"stateOrProvince" db:"state_or_province"`
+	Tenant               *string    `json:"tenant"`
+	TenantID             int        `json:"tenantId" db:"tenant_id"`
+	Token                *string    `json:"-" db:"token"`
+	UID                  *int       `json:"uid"`
+	Username             string     `json:"username" db:"username"`
+}
+
+// UsersResponseV50 is the type of a response from Traffic Ops to requests made
+// to /users which return more than one user.
+type UsersResponseV50 struct {
+	Response []UserV50 `json:"response"`
+	Alerts
+}
+
+// UserResponseV50 is the type of a response from Traffic Ops to requests made
+// to /users which return one user.
+type UserResponseV50 struct {
+	Response UserV50 `json:"response"`
+	Alerts
+}
+
+// copyStringIfNotNil makes a deep copy of s - unless it's nil, in which case it
+// just returns nil.
+func copyStringIfNotNil(s *string) *string {
+	if s == nil {
+		return nil
+	}
+	ret := new(string)
+	*ret = *s
+	return ret
+}
+
+// copyIntIfNotNil makes a deep copy of i - unless it's nil, in which case it
+// just returns nil.
+func copyIntIfNotNil(i *int) *int {
+	if i == nil {
+		return nil
+	}
+	ret := new(int)
+	*ret = *i
+	return ret
+}
+
+// UpgradeFromLegacyUser converts a User to a UserV50 (as seen in API versions 5.x)
+func (u User) UpgradeFromLegacyUser() UserV50 {
+	var ret UserV50
+	ret.AddressLine1 = copyStringIfNotNil(u.AddressLine1)
+	ret.AddressLine2 = copyStringIfNotNil(u.AddressLine2)
+	ret.City = copyStringIfNotNil(u.City)
+	ret.Company = copyStringIfNotNil(u.Company)
+	ret.ConfirmLocalPassword = copyStringIfNotNil(u.ConfirmLocalPassword)
+	ret.Country = copyStringIfNotNil(u.Country)
+	ret.Email = copyStringIfNotNil(u.Email)
+	ret.GID = copyIntIfNotNil(u.GID)
+	ret.ID = copyIntIfNotNil(u.ID)
+	ret.LocalPassword = copyStringIfNotNil(u.LocalPassword)
+	ret.PhoneNumber = copyStringIfNotNil(u.PhoneNumber)
+	ret.PostalCode = copyStringIfNotNil(u.PostalCode)
+	ret.PublicSSHKey = copyStringIfNotNil(u.PublicSSHKey)
+	ret.StateOrProvince = copyStringIfNotNil(u.StateOrProvince)
+	ret.Tenant = copyStringIfNotNil(u.Tenant)
+	ret.Token = copyStringIfNotNil(u.Token)
+	ret.UID = copyIntIfNotNil(u.UID)
+	ret.FullName = u.FullName
+	if u.LastUpdated != nil {
+		ret.LastUpdated = u.LastUpdated.Time
+	}
+	if u.NewUser != nil {
+		ret.NewUser = *u.NewUser
+	}
+	if u.RegistrationSent != nil {
+		ret.RegistrationSent = new(time.Time)
+		*ret.RegistrationSent = u.RegistrationSent.Time
+	}
+	if u.RoleName != nil {
+		ret.Role = *u.RoleName
+	}
+	if u.TenantID != nil {
+		ret.TenantID = *u.TenantID
+	}
+	if u.Username != nil {
+		ret.Username = *u.Username
+	}
+	return ret
+}
+
+// UpgradeFromUserV40 converts a UserV40 to a UserV50 (as seen in API versions 5.x)
+func (u UserV40) UpgradeFromUserV40() UserV50 {
+	var ret UserV50
+	ret.AddressLine1 = copyStringIfNotNil(u.AddressLine1)
+	ret.AddressLine2 = copyStringIfNotNil(u.AddressLine2)
+	ret.City = copyStringIfNotNil(u.City)
+	ret.Company = copyStringIfNotNil(u.Company)
+	ret.ConfirmLocalPassword = copyStringIfNotNil(u.ConfirmLocalPassword)
+	ret.Country = copyStringIfNotNil(u.Country)
+	ret.Email = copyStringIfNotNil(u.Email)
+	ret.GID = copyIntIfNotNil(u.GID)
+	ret.ID = copyIntIfNotNil(u.ID)
+	ret.LocalPassword = copyStringIfNotNil(u.LocalPassword)
+	ret.PhoneNumber = copyStringIfNotNil(u.PhoneNumber)
+	ret.PostalCode = copyStringIfNotNil(u.PostalCode)
+	ret.PublicSSHKey = copyStringIfNotNil(u.PublicSSHKey)
+	ret.StateOrProvince = copyStringIfNotNil(u.StateOrProvince)
+	ret.Tenant = copyStringIfNotNil(u.Tenant)
+	ret.Token = copyStringIfNotNil(u.Token)
+	ret.UID = copyIntIfNotNil(u.UID)
+	ret.ChangeLogCount = copyIntIfNotNil(u.ChangeLogCount)
+	if u.LastAuthenticated != nil {
+		ret.LastAuthenticated = *u.LastAuthenticated
+	}
+	ret.FullName = u.FullName
+	if u.LastUpdated != nil {
+		ret.LastUpdated = u.LastUpdated.Time
+	}
+	if u.NewUser != nil {
+		ret.NewUser = *u.NewUser
+	}
+	if u.RegistrationSent != nil {
+		ret.RegistrationSent = new(time.Time)
+		*ret.RegistrationSent = u.RegistrationSent.Time
+	}
+	if u.RoleName != nil {
+		ret.Role = *u.RoleName
+	}
+	if u.TenantID != nil {
+		ret.TenantID = *u.TenantID
+	}
+	if u.Username != nil {
+		ret.Username = *u.Username
+	}
+	return ret
+}
+
+// Downgrade converts a UserV50 to a UserV40 (as seen in API versions 4.x) and User (as seen in API versions < 4.0)
+func (u UserV50) Downgrade() (UserV40, User) {

Review comment:
       Since most of the struct params in `User` and `UserV40` are the same, I took care of both downgrades in one function, instead of duplicating the code. I don't mind changing it to what you're suggesting. Thoughts?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org