Remove useless User struct
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -83,9 +83,3 @@ func ParseUserFilter(expr scimfilter.Expression) (*coredata.MembershipFilter, er
|
|||||||
|
|
||||||
return filter, nil
|
return filter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
|
||||||
Email string
|
|
||||||
FullName string
|
|
||||||
Active *bool
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -96,8 +96,7 @@ func (s *Service) CreateUser(
|
|||||||
attributes scim.ResourceAttributes,
|
attributes scim.ResourceAttributes,
|
||||||
ipAddress net.IP,
|
ipAddress net.IP,
|
||||||
) (scim.Resource, error) {
|
) (scim.Resource, error) {
|
||||||
user := ParseUserFromAttributes(attributes)
|
email, fullName := ParseUserFromAttributes(attributes)
|
||||||
email := user.Email
|
|
||||||
if email == "" {
|
if email == "" {
|
||||||
return scim.Resource{}, scimerrors.ScimErrorBadRequest("userName or email is required")
|
return scim.Resource{}, scimerrors.ScimErrorBadRequest("userName or email is required")
|
||||||
}
|
}
|
||||||
@@ -106,8 +105,6 @@ func (s *Service) CreateUser(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return scim.Resource{}, scimerrors.ScimErrorBadRequest("invalid email format")
|
return scim.Resource{}, scimerrors.ScimErrorBadRequest("invalid email format")
|
||||||
}
|
}
|
||||||
|
|
||||||
fullName := user.FullName
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
var membership *coredata.Membership
|
var membership *coredata.Membership
|
||||||
@@ -302,8 +299,8 @@ func (s *Service) ReplaceUser(
|
|||||||
attributes scim.ResourceAttributes,
|
attributes scim.ResourceAttributes,
|
||||||
ipAddress net.IP,
|
ipAddress net.IP,
|
||||||
) (scim.Resource, error) {
|
) (scim.Resource, error) {
|
||||||
user := ParseUserFromReplaceAttributes(attributes)
|
fullName, active := ParseUserFromReplaceAttributes(attributes)
|
||||||
membership, deactivated, err := s.updateUser(ctx, config, membershipID, user, "PUT", ipAddress)
|
membership, deactivated, err := s.updateUser(ctx, config, membershipID, fullName, active, "PUT", ipAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return scim.Resource{}, err
|
return scim.Resource{}, err
|
||||||
}
|
}
|
||||||
@@ -318,8 +315,8 @@ func (s *Service) PatchUser(
|
|||||||
operations []scim.PatchOperation,
|
operations []scim.PatchOperation,
|
||||||
ipAddress net.IP,
|
ipAddress net.IP,
|
||||||
) (scim.Resource, error) {
|
) (scim.Resource, error) {
|
||||||
user := ParseUserFromPatchOperations(operations)
|
fullName, active := ParseUserFromPatchOperations(operations)
|
||||||
membership, deactivated, err := s.updateUser(ctx, config, membershipID, user, "PATCH", ipAddress)
|
membership, deactivated, err := s.updateUser(ctx, config, membershipID, fullName, active, "PATCH", ipAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return scim.Resource{}, err
|
return scim.Resource{}, err
|
||||||
}
|
}
|
||||||
@@ -330,7 +327,8 @@ func (s *Service) updateUser(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
config *coredata.SCIMConfiguration,
|
config *coredata.SCIMConfiguration,
|
||||||
membershipID gid.GID,
|
membershipID gid.GID,
|
||||||
user *User,
|
fullName string,
|
||||||
|
active *bool,
|
||||||
method string,
|
method string,
|
||||||
ipAddress net.IP,
|
ipAddress net.IP,
|
||||||
) (*coredata.Membership, bool, error) {
|
) (*coredata.Membership, bool, error) {
|
||||||
@@ -356,7 +354,7 @@ func (s *Service) updateUser(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle deactivation - Okta sends PATCH with active=false to deprovision users
|
// Handle deactivation - Okta sends PATCH with active=false to deprovision users
|
||||||
if user.Active != nil && !*user.Active {
|
if active != nil && !*active {
|
||||||
err = membership.Delete(ctx, tx, scope, membershipID)
|
err = membership.Delete(ctx, tx, scope, membershipID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot delete membership: %w", err)
|
return fmt.Errorf("cannot delete membership: %w", err)
|
||||||
@@ -389,7 +387,6 @@ func (s *Service) updateUser(
|
|||||||
profile := &coredata.MembershipProfile{}
|
profile := &coredata.MembershipProfile{}
|
||||||
err = profile.LoadByMembershipID(ctx, tx, scope, membershipID)
|
err = profile.LoadByMembershipID(ctx, tx, scope, membershipID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fullName := user.FullName
|
|
||||||
if fullName != "" {
|
if fullName != "" {
|
||||||
profile.FullName = fullName
|
profile.FullName = fullName
|
||||||
profile.UpdatedAt = now
|
profile.UpdatedAt = now
|
||||||
@@ -515,9 +512,8 @@ func (s *Service) createEvent(
|
|||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseUserFromAttributes extracts a User from SCIM resource attributes
|
|
||||||
// ParseUserFromAttributes extracts user data from SCIM create attributes
|
// ParseUserFromAttributes extracts user data from SCIM create attributes
|
||||||
func ParseUserFromAttributes(attributes scim.ResourceAttributes) *User {
|
func ParseUserFromAttributes(attributes scim.ResourceAttributes) (email string, fullName string) {
|
||||||
userName, _ := attributes["userName"].(string)
|
userName, _ := attributes["userName"].(string)
|
||||||
displayName, _ := attributes["displayName"].(string)
|
displayName, _ := attributes["displayName"].(string)
|
||||||
|
|
||||||
@@ -528,7 +524,7 @@ func ParseUserFromAttributes(attributes scim.ResourceAttributes) *User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get email from emails array or use userName
|
// Get email from emails array or use userName
|
||||||
email := userName
|
email = userName
|
||||||
if emails, ok := attributes["emails"].([]interface{}); ok && len(emails) > 0 {
|
if emails, ok := attributes["emails"].([]interface{}); ok && len(emails) > 0 {
|
||||||
for _, e := range emails {
|
for _, e := range emails {
|
||||||
if emailMap, ok := e.(map[string]interface{}); ok {
|
if emailMap, ok := e.(map[string]interface{}); ok {
|
||||||
@@ -551,7 +547,7 @@ func ParseUserFromAttributes(attributes scim.ResourceAttributes) *User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build full name: prefer displayName, then given+family, then userName
|
// Build full name: prefer displayName, then given+family, then userName
|
||||||
fullName := displayName
|
fullName = displayName
|
||||||
if fullName == "" {
|
if fullName == "" {
|
||||||
fullName = strings.TrimSpace(givenName + " " + familyName)
|
fullName = strings.TrimSpace(givenName + " " + familyName)
|
||||||
}
|
}
|
||||||
@@ -559,14 +555,11 @@ func ParseUserFromAttributes(attributes scim.ResourceAttributes) *User {
|
|||||||
fullName = userName
|
fullName = userName
|
||||||
}
|
}
|
||||||
|
|
||||||
return &User{
|
return email, fullName
|
||||||
Email: email,
|
|
||||||
FullName: fullName,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseUserFromReplaceAttributes extracts user data from SCIM replace (PUT) attributes
|
// ParseUserFromReplaceAttributes extracts user data from SCIM replace (PUT) attributes
|
||||||
func ParseUserFromReplaceAttributes(attributes scim.ResourceAttributes) *User {
|
func ParseUserFromReplaceAttributes(attributes scim.ResourceAttributes) (fullName string, active *bool) {
|
||||||
displayName, _ := attributes["displayName"].(string)
|
displayName, _ := attributes["displayName"].(string)
|
||||||
|
|
||||||
var givenName, familyName string
|
var givenName, familyName string
|
||||||
@@ -575,25 +568,21 @@ func ParseUserFromReplaceAttributes(attributes scim.ResourceAttributes) *User {
|
|||||||
familyName, _ = name["familyName"].(string)
|
familyName, _ = name["familyName"].(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
fullName := displayName
|
fullName = displayName
|
||||||
if fullName == "" {
|
if fullName == "" {
|
||||||
fullName = strings.TrimSpace(givenName + " " + familyName)
|
fullName = strings.TrimSpace(givenName + " " + familyName)
|
||||||
}
|
}
|
||||||
|
|
||||||
active := true
|
activeVal := true
|
||||||
if a, ok := attributes["active"].(bool); ok {
|
if a, ok := attributes["active"].(bool); ok {
|
||||||
active = a
|
activeVal = a
|
||||||
}
|
}
|
||||||
|
|
||||||
return &User{
|
return fullName, &activeVal
|
||||||
FullName: fullName,
|
|
||||||
Active: &active,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseUserFromPatchOperations extracts user data from SCIM patch operations
|
// ParseUserFromPatchOperations extracts user data from SCIM patch operations
|
||||||
func ParseUserFromPatchOperations(operations []scim.PatchOperation) *User {
|
func ParseUserFromPatchOperations(operations []scim.PatchOperation) (fullName string, active *bool) {
|
||||||
user := &User{}
|
|
||||||
var givenName, familyName string
|
var givenName, familyName string
|
||||||
|
|
||||||
for _, op := range operations {
|
for _, op := range operations {
|
||||||
@@ -604,12 +593,12 @@ func ParseUserFromPatchOperations(operations []scim.PatchOperation) *User {
|
|||||||
}
|
}
|
||||||
switch strings.ToLower(path) {
|
switch strings.ToLower(path) {
|
||||||
case "active":
|
case "active":
|
||||||
if active, ok := op.Value.(bool); ok {
|
if a, ok := op.Value.(bool); ok {
|
||||||
user.Active = &active
|
active = &a
|
||||||
}
|
}
|
||||||
case "displayname":
|
case "displayname":
|
||||||
if name, ok := op.Value.(string); ok {
|
if name, ok := op.Value.(string); ok {
|
||||||
user.FullName = name
|
fullName = name
|
||||||
}
|
}
|
||||||
case "name.givenname":
|
case "name.givenname":
|
||||||
if name, ok := op.Value.(string); ok {
|
if name, ok := op.Value.(string); ok {
|
||||||
@@ -624,11 +613,11 @@ func ParseUserFromPatchOperations(operations []scim.PatchOperation) *User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If no displayName was set but we have name parts, build full name
|
// If no displayName was set but we have name parts, build full name
|
||||||
if user.FullName == "" && (givenName != "" || familyName != "") {
|
if fullName == "" && (givenName != "" || familyName != "") {
|
||||||
user.FullName = strings.TrimSpace(givenName + " " + familyName)
|
fullName = strings.TrimSpace(givenName + " " + familyName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return user
|
return fullName, active
|
||||||
}
|
}
|
||||||
|
|
||||||
func membershipToResource(m *coredata.Membership, active bool) scim.Resource {
|
func membershipToResource(m *coredata.Membership, active bool) scim.Resource {
|
||||||
@@ -639,10 +628,10 @@ func membershipToResource(m *coredata.Membership, active bool) scim.Resource {
|
|||||||
"userName": m.EmailAddress.String(),
|
"userName": m.EmailAddress.String(),
|
||||||
"displayName": m.FullName,
|
"displayName": m.FullName,
|
||||||
"active": active,
|
"active": active,
|
||||||
"name": map[string]interface{}{
|
"name": map[string]any{
|
||||||
"formatted": m.FullName,
|
"formatted": m.FullName,
|
||||||
},
|
},
|
||||||
"emails": []map[string]interface{}{
|
"emails": []map[string]any{
|
||||||
{
|
{
|
||||||
"value": m.EmailAddress.String(),
|
"value": m.EmailAddress.String(),
|
||||||
"type": "work",
|
"type": "work",
|
||||||
|
|||||||
Reference in New Issue
Block a user