From 1b5a41f68332acab3c6a4c13994e3a61c7d97b86 Mon Sep 17 00:00:00 2001 From: Rob Archibald Date: Tue, 24 Jan 2017 15:15:50 -0800 Subject: [PATCH] include db login id in CreateLogin. Fix LDAP syntax in create --- authStore.go | 6 +++--- backend.go | 8 ++++---- backendLDAPLogin.go | 13 +++++++------ backendLDAPLogin_test.go | 2 +- backendMemory.go | 2 +- backendMemory_test.go | 2 +- backend_test.go | 4 ++-- 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/authStore.go b/authStore.go index 1ae8369..06a9454 100644 --- a/authStore.go +++ b/authStore.go @@ -350,7 +350,7 @@ func (s *authStore) createProfile(fullName, organization, password, picturePath return newLoggedError("Error while creating profile", err) } - _, err = s.createLogin(session.UserID, session.Email, fullName, password, mailQuota, fileQuota) + _, err = s.createLogin(session.UserID, session.UserID, session.Email, fullName, password, mailQuota, fileQuota) if err != nil { return newLoggedError("Unable to create login", err) } @@ -365,7 +365,7 @@ func (s *authStore) createProfile(fullName, organization, password, picturePath } /**************** TODO: send 0 for UID and GID numbers and empty quotas if mailQuota and fileQuota are 0 **********************/ -func (s *authStore) createLogin(userID int, email, fullName, password string, mailQuota, fileQuota int) (*userLogin, error) { +func (s *authStore) createLogin(userID, dbUserID int, email, fullName, password string, mailQuota, fileQuota int) (*userLogin, error) { passwordHash, err := cryptoHash(password) if err != nil { return nil, newLoggedError("Unable to create login", err) @@ -376,7 +376,7 @@ func (s *authStore) createLogin(userID int, email, fullName, password string, ma homeDirectory := "/home" mQuota := fmt.Sprintf("%dGB", mailQuota) fQuota := fmt.Sprintf("%dGB", fileQuota) - login, err := s.backend.CreateLogin(userID, email, passwordHash, fullName, homeDirectory, uidNumber, gidNumber, mQuota, fQuota) + login, err := s.backend.CreateLogin(userID, dbUserID, email, passwordHash, fullName, homeDirectory, uidNumber, gidNumber, mQuota, fQuota) if err != nil { return nil, newLoggedError("Unable to create login", err) } diff --git a/backend.go b/backend.go index cb027db..48f7067 100644 --- a/backend.go +++ b/backend.go @@ -27,7 +27,7 @@ type backender interface { UpdateUser(email, fullname string, company string, pictureURL string) error // LoginBackender. Write out since it contains duplicate BackendCloser - CreateLogin(userID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) + CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) Login(email, password string) (*userLogin, error) UpdateEmail(email string, password string, newEmail string) (*loginSession, error) UpdatePassword(email string, oldPassword string, newPassword string) (*loginSession, error) @@ -47,7 +47,7 @@ type userBackender interface { } type loginBackender interface { - CreateLogin(userID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) + CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) Login(email, password string) (*userLogin, error) UpdateEmail(email string, password string, newEmail string) (*loginSession, error) UpdatePassword(email string, oldPassword string, newPassword string) (*loginSession, error) @@ -212,8 +212,8 @@ func (b *backend) UpdateUser(email, fullname string, company string, pictureURL return b.u.UpdateUser(email, fullname, company, pictureURL) } -func (b *backend) CreateLogin(userID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) { - return b.l.CreateLogin(userID, email, passwordHash, fullName, homeDirectory, uidNumber, gidNumber, mailQuota, fileQuota) +func (b *backend) CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) { + return b.l.CreateLogin(userID, dbUserID, email, passwordHash, fullName, homeDirectory, uidNumber, gidNumber, mailQuota, fileQuota) } func (b *backend) UpdateEmail(email string, password string, newEmail string) (*loginSession, error) { diff --git a/backendLDAPLogin.go b/backendLDAPLogin.go index 3c44fb7..3be66ad 100644 --- a/backendLDAPLogin.go +++ b/backendLDAPLogin.go @@ -29,7 +29,7 @@ type ldapData struct { func (l *backendLDAPLogin) Login(email, password string) (*userLogin, error) { // check credentials - err := l.db.Execute(ldap.NewSimpleBindRequest(email, password, nil)) + err := l.db.Execute(ldap.NewSimpleBindRequest(fmt.Sprintf("uid=%s,%s", email, l.baseDn), password, nil)) if err != nil { return nil, err } @@ -48,17 +48,18 @@ func (l *backendLDAPLogin) Login(email, password string) (*userLogin, error) { } /**************** TODO: create different type of user if not using file and mail quotas **********************/ -func (l *backendLDAPLogin) CreateLogin(userID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) { +func (l *backendLDAPLogin) CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) { req := ldap.NewAddRequest("uid=" + email + ",ou=Users,dc=endfirst,dc=com") - req.Attribute("objectClass", []string{"posixAccount", "account", "ownCloud", "systemQuotas"}) + req.Attribute("objectClass", []string{"endfirstAccount", "endfirstSubscriber"}) req.Attribute("uid", []string{email}) + req.Attribute("dbUserId", []string{strconv.Itoa(dbUserID)}) req.Attribute("cn", []string{fullName}) req.Attribute("userPassword", []string{passwordHash}) req.Attribute("uidNumber", []string{strconv.Itoa(uidNumber)}) req.Attribute("gidNumber", []string{strconv.Itoa(gidNumber)}) - req.Attribute("homeDirectory", []string{homeDirectory}) - req.Attribute("quota", []string{mailQuota}) - req.Attribute("ownCloudQuota", []string{fileQuota}) + req.Attribute("mailFolder", []string{homeDirectory}) + req.Attribute("mailQuota", []string{mailQuota}) + req.Attribute("fileQuota", []string{fileQuota}) err := l.db.Execute(req) return &userLogin{}, err } diff --git a/backendLDAPLogin_test.go b/backendLDAPLogin_test.go index d76016d..ae811f5 100644 --- a/backendLDAPLogin_test.go +++ b/backendLDAPLogin_test.go @@ -59,7 +59,7 @@ func TestLdapLogin(t *testing.T) { func TestLdapCreateLogin(t *testing.T) { m := onedb.NewMock(nil, nil, nil) l := backendLDAPLogin{db: m} - _, err := l.CreateLogin(1, "email", "hash", "name", "homeDir", 1, 1, "mailQuota", "fileQuota") + _, err := l.CreateLogin(1, 1, "email", "hash", "name", "homeDir", 1, 1, "mailQuota", "fileQuota") if err != nil { t.Error("expected success") } diff --git a/backendMemory.go b/backendMemory.go index 26c5272..c2b79ba 100644 --- a/backendMemory.go +++ b/backendMemory.go @@ -166,7 +166,7 @@ func (m *backendMemory) UpdateUser(email, fullname string, company string, pictu return nil } -func (m *backendMemory) CreateLogin(userID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) { +func (m *backendMemory) CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) { login := userLoginMemory{userID, email, fullName, passwordHash} m.Logins = append(m.Logins, &login) diff --git a/backendMemory_test.go b/backendMemory_test.go index f517448..4c49aa5 100644 --- a/backendMemory_test.go +++ b/backendMemory_test.go @@ -164,7 +164,7 @@ func TestMemoryUpdateUser(t *testing.T) { func TestMemoryCreateLogin(t *testing.T) { backend := newBackendMemory().(*backendMemory) - if login, err := backend.CreateLogin(1, "email", "passwordHash", "fullName", "homeDirectory", 1, 1, "mailQuota", "fileQuota"); err != nil || login.Email != "email" { + if login, err := backend.CreateLogin(1, 1, "email", "passwordHash", "fullName", "homeDirectory", 1, 1, "mailQuota", "fileQuota"); err != nil || login.Email != "email" { t.Error("expected valid login", login) } } diff --git a/backend_test.go b/backend_test.go index 71b8cf5..8c11a32 100644 --- a/backend_test.go +++ b/backend_test.go @@ -102,7 +102,7 @@ func TestBackendUpdateUser(t *testing.T) { func TestBackendCreateLogin(t *testing.T) { m := &mockBackend{CreateLoginReturn: loginErr()} b := backend{u: m, l: m, s: m} - b.CreateLogin(1, "email", "hash", "name", "homeDir", 1, 1, "quota", "fileQuota") + b.CreateLogin(1, 1, "email", "hash", "name", "homeDir", 1, 1, "quota", "fileQuota") if len(m.MethodsCalled) != 1 || m.MethodsCalled[0] != "CreateLogin" { t.Error("Expected it would call backend", m.MethodsCalled) } @@ -331,7 +331,7 @@ func (b *mockBackend) UpdateUser(email, fullname, company, pictureURL string) er return b.ErrReturn } -func (b *mockBackend) CreateLogin(userID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) { +func (b *mockBackend) CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) { b.MethodsCalled = append(b.MethodsCalled, "CreateLogin") if b.CreateLoginReturn == nil { return nil, errors.New("CreateLoginReturn not initialized")