Skip to content

Commit

Permalink
update user if it already exists in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Jan 15, 2025
1 parent 6ec1f98 commit 49df727
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 27 deletions.
1 change: 1 addition & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ LDAP_BIND_USER=CN=user,DC=example,DC=com
LDAP_BIND_PASSWORD=securepasswordhere
LDAP_SEARCH_BASE=OU=Stuff,DC=example,DC=com
LDAP_TLS_VERIFY=false
LDAP_USER_ID_ATTRIBUTE=uid
LDAP_USERNAME_ATTRIBUTE=uid
LDAP_GROUP_ATTRIBUTE=uid
2 changes: 2 additions & 0 deletions backend/internal/common/env_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type EnvConfigSchema struct {
LDAPBindPassword string `env:"LDAP_BIND_PASSWORD"`
LDAPSearchBase string `env:"LDAP_SEARCH_BASE"`
LDAPTLSVerify bool `env:"LDAP_TLS_VERIFY"`
LDAPUserIdAttribute string `env:"LDAP_USER_ID_ATTRIBUTE"`
LDAPUsernameAttribute string `env:"LDAP_USERNAME_ATTRIBUTE"`
LDAPGroupAttribute string `env:"LDAP_GROUP_ATTRIBUTE"`
}
Expand All @@ -50,6 +51,7 @@ var EnvConfig = &EnvConfigSchema{
LDAPBindPassword: "",
LDAPSearchBase: "",
LDAPTLSVerify: false,
LDAPUserIdAttribute: "",
LDAPUsernameAttribute: "",
LDAPGroupAttribute: "",
}
Expand Down
1 change: 1 addition & 0 deletions backend/internal/dto/user_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type UserCreateDto struct {
FirstName string `json:"firstName" binding:"required,min=1,max=50"`
LastName string `json:"lastName" binding:"required,min=1,max=50"`
IsAdmin bool `json:"isAdmin"`
LdapID string `json:"-"`
}

type OneTimeAccessTokenCreateDto struct {
Expand Down
1 change: 1 addition & 0 deletions backend/internal/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type User struct {
FirstName string `sortable:"true"`
LastName string `sortable:"true"`
IsAdmin bool `sortable:"true"`
LdapID *string

CustomClaims []CustomClaim
UserGroups []UserGroup `gorm:"many2many:user_groups_users;"`
Expand Down
52 changes: 25 additions & 27 deletions backend/internal/service/ldap_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"crypto/tls"
"fmt"
"log"

"github.com/go-ldap/ldap/v3"
"github.com/stonith404/pocket-id/backend/internal/common"
Expand Down Expand Up @@ -100,41 +101,38 @@ func (s *LdapService) GetLdapUsers() error {
fmt.Println(fmt.Errorf("failed to query LDAP: %w", err))
}

var userError error
for _, value := range result.Entries {
ldapId := value.GetAttributeValue(common.EnvConfig.LDAPUserIdAttribute)

if len(result.Entries) >= 1 {
// Get the user from the database
var databaseUser model.User
s.db.Where("ldap_id = ?", ldapId).First(&databaseUser)

for _, value := range result.Entries {
newUser := dto.UserCreateDto{
Username: value.GetAttributeValue(common.EnvConfig.LDAPUsernameAttribute),
Email: value.GetAttributeValue("mail"),
FirstName: value.GetAttributeValue("givenName"),
LastName: value.GetAttributeValue("sn"),
IsAdmin: false,
LdapID: ldapId,
}

newUserModel := model.User{
Username: value.GetAttributeValue(common.EnvConfig.LDAPUsernameAttribute),
Email: value.GetAttributeValue("mail"),
FirstName: value.GetAttributeValue("givenName"),
LastName: value.GetAttributeValue("sn"),
IsAdmin: false,
if databaseUser.ID == "" {
_, err = s.userService.CreateUser(newUser)
if err != nil {
log.Printf("Error syncing user %s: %s", newUser.Username, err)
}

if s.userService.checkDuplicatedFields(newUserModel) == nil {
newUser := dto.UserCreateDto{
Username: value.GetAttributeValue(common.EnvConfig.LDAPUsernameAttribute),
Email: value.GetAttributeValue("mail"),
FirstName: value.GetAttributeValue("givenName"),
LastName: value.GetAttributeValue("sn"),
IsAdmin: false,
}
_, userError = s.userService.CreateUser(newUser)
} else {
// Update Exsisting User Entry Logic here.
} else {
_, err = s.userService.UpdateUser(databaseUser.ID, newUser, false)
if err != nil {
log.Printf("Error syncing user %s: %s", newUser.Username, err)
}

}

client.Close()
return userError

} else {
fmt.Println("No Users Found")
return userError
}

client.Close()
return nil

}
1 change: 1 addition & 0 deletions backend/internal/service/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (s *UserService) CreateUser(input dto.UserCreateDto) (model.User, error) {
Email: input.Email,
Username: input.Username,
IsAdmin: input.IsAdmin,
LdapID: &input.LdapID,
}
if err := s.db.Create(&user).Error; err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users DROP COLUMN ldap_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN ldap_id TEXT;

0 comments on commit 49df727

Please sign in to comment.