From 25d2a2bc7174e6a86413d42ff9d8fbda0ee858c8 Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Tue, 7 Feb 2023 13:21:42 +0100 Subject: [PATCH] graph: Remove some uneeded stuff for the education backend We don't need to support any complex queries on /education (yet?). And if we would need to add support for $search, $filter, $expand or $select we should pass the parsed odata Query instead of the raw url.Values struct. --- services/graph/pkg/identity/backend.go | 14 +-- services/graph/pkg/identity/err_education.go | 13 ++- .../pkg/identity/ldap_education_class.go | 22 +---- .../pkg/identity/ldap_education_class_test.go | 9 +- .../pkg/identity/ldap_education_school.go | 5 +- .../identity/ldap_education_school_test.go | 4 +- .../graph/pkg/identity/ldap_education_user.go | 22 +---- .../pkg/identity/ldap_education_user_test.go | 6 +- .../pkg/identity/mocks/education_backend.go | 86 +++++++++---------- .../graph/pkg/service/v0/educationclasses.go | 4 +- .../graph/pkg/service/v0/educationschools.go | 4 +- .../graph/pkg/service/v0/educationuser.go | 68 +-------------- .../pkg/service/v0/educationuser_test.go | 76 ---------------- 13 files changed, 81 insertions(+), 252 deletions(-) diff --git a/services/graph/pkg/identity/backend.go b/services/graph/pkg/identity/backend.go index d8c9d877d1..cca15dc56b 100644 --- a/services/graph/pkg/identity/backend.go +++ b/services/graph/pkg/identity/backend.go @@ -48,9 +48,9 @@ type EducationBackend interface { // DeleteSchool deletes a given school, identified by id DeleteEducationSchool(ctx context.Context, id string) error // GetEducationSchool reads a given school by id - GetEducationSchool(ctx context.Context, nameOrID string, queryParam url.Values) (*libregraph.EducationSchool, error) + GetEducationSchool(ctx context.Context, nameOrID string) (*libregraph.EducationSchool, error) // GetEducationSchools lists all schools - GetEducationSchools(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationSchool, error) + GetEducationSchools(ctx context.Context) ([]*libregraph.EducationSchool, error) // UpdateEducationSchool updates attributes of a school UpdateEducationSchool(ctx context.Context, numberOrID string, school libregraph.EducationSchool) (*libregraph.EducationSchool, error) // GetEducationSchoolUsers lists all members of a school @@ -68,9 +68,9 @@ type EducationBackend interface { RemoveClassFromEducationSchool(ctx context.Context, schoolNumberOrID string, memberID string) error // GetEducationClasses lists all classes - GetEducationClasses(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationClass, error) + GetEducationClasses(ctx context.Context) ([]*libregraph.EducationClass, error) // GetEducationClasses reads a given class by id - GetEducationClass(ctx context.Context, namedOrID string, queryParam url.Values) (*libregraph.EducationClass, error) + GetEducationClass(ctx context.Context, namedOrID string) (*libregraph.EducationClass, error) // CreateEducationClass creates the supplied education class in the identity backend. CreateEducationClass(ctx context.Context, class libregraph.EducationClass) (*libregraph.EducationClass, error) // DeleteEducationClass deletes the supplied education class in the identity backend. @@ -86,8 +86,10 @@ type EducationBackend interface { DeleteEducationUser(ctx context.Context, nameOrID string) error // UpdateEducationUser applies changes to given education user, identified by username or id UpdateEducationUser(ctx context.Context, nameOrID string, user libregraph.EducationUser) (*libregraph.EducationUser, error) - GetEducationUser(ctx context.Context, nameOrID string, queryParam url.Values) (*libregraph.EducationUser, error) - GetEducationUsers(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationUser, error) + // GetEducationUser reads an education user by id or name + GetEducationUser(ctx context.Context, nameOrID string) (*libregraph.EducationUser, error) + // GetEducationUsers lists all education users + GetEducationUsers(ctx context.Context) ([]*libregraph.EducationUser, error) } func CreateUserModelFromCS3(u *cs3.User) *libregraph.User { diff --git a/services/graph/pkg/identity/err_education.go b/services/graph/pkg/identity/err_education.go index 323e01d793..e68e2e030c 100644 --- a/services/graph/pkg/identity/err_education.go +++ b/services/graph/pkg/identity/err_education.go @@ -2,7 +2,6 @@ package identity import ( "context" - "net/url" libregraph "github.com/owncloud/libre-graph-api-go" ) @@ -21,12 +20,12 @@ func (i *ErrEducationBackend) DeleteEducationSchool(ctx context.Context, id stri } // GetEducationSchool implements the EducationBackend interface for the ErrEducationBackend backend. -func (i *ErrEducationBackend) GetEducationSchool(ctx context.Context, nameOrID string, queryParam url.Values) (*libregraph.EducationSchool, error) { +func (i *ErrEducationBackend) GetEducationSchool(ctx context.Context, nameOrID string) (*libregraph.EducationSchool, error) { return nil, errNotImplemented } // GetEducationSchools implements the EducationBackend interface for the ErrEducationBackend backend. -func (i *ErrEducationBackend) GetEducationSchools(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationSchool, error) { +func (i *ErrEducationBackend) GetEducationSchools(ctx context.Context) ([]*libregraph.EducationSchool, error) { return nil, errNotImplemented } @@ -66,12 +65,12 @@ func (i *ErrEducationBackend) RemoveUserFromEducationSchool(ctx context.Context, } // GetEducationClasses implements the EducationBackend interface -func (i *ErrEducationBackend) GetEducationClasses(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationClass, error) { +func (i *ErrEducationBackend) GetEducationClasses(ctx context.Context) ([]*libregraph.EducationClass, error) { return nil, errNotImplemented } // GetEducationClass implements the EducationBackend interface -func (i *ErrEducationBackend) GetEducationClass(ctx context.Context, namedOrID string, queryParam url.Values) (*libregraph.EducationClass, error) { +func (i *ErrEducationBackend) GetEducationClass(ctx context.Context, namedOrID string) (*libregraph.EducationClass, error) { return nil, errNotImplemented } @@ -111,11 +110,11 @@ func (i *ErrEducationBackend) UpdateEducationUser(ctx context.Context, nameOrID } // GetEducationUser implements the EducationBackend interface for the ErrEducationBackend backend. -func (i *ErrEducationBackend) GetEducationUser(ctx context.Context, nameOrID string, queryParam url.Values) (*libregraph.EducationUser, error) { +func (i *ErrEducationBackend) GetEducationUser(ctx context.Context, nameOrID string) (*libregraph.EducationUser, error) { return nil, errNotImplemented } // GetEducationUsers implements the EducationBackend interface for the ErrEducationBackend backend. -func (i *ErrEducationBackend) GetEducationUsers(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationUser, error) { +func (i *ErrEducationBackend) GetEducationUsers(ctx context.Context) ([]*libregraph.EducationUser, error) { return nil, errNotImplemented } diff --git a/services/graph/pkg/identity/ldap_education_class.go b/services/graph/pkg/identity/ldap_education_class.go index 96be43e0e0..3ea687c462 100644 --- a/services/graph/pkg/identity/ldap_education_class.go +++ b/services/graph/pkg/identity/ldap_education_class.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "net/url" "github.com/go-ldap/ldap/v3" libregraph "github.com/owncloud/libre-graph-api-go" @@ -25,26 +24,11 @@ func newEducationClassAttributeMap() educationClassAttributeMap { } // GetEducationClasses implements the EducationBackend interface for the LDAP backend. -func (i *LDAP) GetEducationClasses(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationClass, error) { +func (i *LDAP) GetEducationClasses(ctx context.Context) ([]*libregraph.EducationClass, error) { logger := i.logger.SubloggerWithRequestID(ctx) logger.Debug().Str("backend", "ldap").Msg("GetEducationClasses") - search := queryParam.Get("search") - if search == "" { - search = queryParam.Get("$search") - } - - var classFilter string - if search != "" { - search = ldap.EscapeFilter(search) - classFilter = fmt.Sprintf( - "(|(%s=%s)(%s=%s*)(%s=%s*))", - i.educationConfig.classAttributeMap.externalID, search, - i.groupAttributeMap.name, search, - i.groupAttributeMap.id, search, - ) - } - classFilter = fmt.Sprintf("(&%s(objectClass=%s)%s)", i.groupFilter, i.educationConfig.classObjectClass, classFilter) + classFilter := fmt.Sprintf("(&%s(objectClass=%s))", i.groupFilter, i.educationConfig.classObjectClass) classAttrs := i.getEducationClassAttrTypes(false) @@ -105,7 +89,7 @@ func (i *LDAP) CreateEducationClass(ctx context.Context, class libregraph.Educat } // GetEducationClass implements the EducationBackend interface for the LDAP backend. -func (i *LDAP) GetEducationClass(ctx context.Context, id string, queryParam url.Values) (*libregraph.EducationClass, error) { +func (i *LDAP) GetEducationClass(ctx context.Context, id string) (*libregraph.EducationClass, error) { logger := i.logger.SubloggerWithRequestID(ctx) logger.Debug().Str("backend", "ldap").Msg("GetEducationClass") e, err := i.getEducationClassByID(id, false) diff --git a/services/graph/pkg/identity/ldap_education_class_test.go b/services/graph/pkg/identity/ldap_education_class_test.go index 8562133d0f..21594310bf 100644 --- a/services/graph/pkg/identity/ldap_education_class_test.go +++ b/services/graph/pkg/identity/ldap_education_class_test.go @@ -3,7 +3,6 @@ package identity import ( "context" "errors" - "net/url" "testing" "github.com/go-ldap/ldap/v3" @@ -72,7 +71,7 @@ func TestGetEducationClasses(t *testing.T) { lm := &mocks.Client{} lm.On("Search", mock.Anything).Return(nil, ldap.NewError(ldap.LDAPResultOperationsError, errors.New("mock"))) b, _ := getMockedBackend(lm, lconfig, &logger) - _, err := b.GetEducationClasses(context.Background(), url.Values{}) + _, err := b.GetEducationClasses(context.Background()) if err == nil || err.Error() != "itemNotFound" { t.Errorf("Expected 'itemNotFound' got '%s'", err.Error()) } @@ -80,7 +79,7 @@ func TestGetEducationClasses(t *testing.T) { lm = &mocks.Client{} lm.On("Search", mock.Anything).Return(&ldap.SearchResult{}, nil) b, _ = getMockedBackend(lm, lconfig, &logger) - g, err := b.GetEducationClasses(context.Background(), url.Values{}) + g, err := b.GetEducationClasses(context.Background()) if err != nil { t.Errorf("Expected success, got '%s'", err.Error()) } else if g == nil || len(g) != 0 { @@ -92,7 +91,7 @@ func TestGetEducationClasses(t *testing.T) { Entries: []*ldap.Entry{classEntry}, }, nil) b, _ = getMockedBackend(lm, lconfig, &logger) - g, err = b.GetEducationClasses(context.Background(), url.Values{}) + g, err = b.GetEducationClasses(context.Background()) if err != nil { t.Errorf("Expected GetEducationClasses to succeed. Got %s", err.Error()) } else if *g[0].Id != classEntry.GetEqualFoldAttributeValue(b.groupAttributeMap.id) { @@ -152,7 +151,7 @@ func TestGetEducationClass(t *testing.T) { b, err := getMockedBackend(lm, eduConfig, &logger) assert.Nil(t, err) - class, err := b.GetEducationClass(context.Background(), tt.id, nil) + class, err := b.GetEducationClass(context.Background(), tt.id) lm.AssertNumberOfCalls(t, "Search", 1) if tt.expectedItemNotFound { diff --git a/services/graph/pkg/identity/ldap_education_school.go b/services/graph/pkg/identity/ldap_education_school.go index d0a44d8a43..884366d386 100644 --- a/services/graph/pkg/identity/ldap_education_school.go +++ b/services/graph/pkg/identity/ldap_education_school.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "net/url" "github.com/go-ldap/ldap/v3" "github.com/gofrs/uuid" @@ -292,7 +291,7 @@ func (i *LDAP) DeleteEducationSchool(ctx context.Context, id string) error { } // GetEducationSchool implements the EducationBackend interface for the LDAP backend. -func (i *LDAP) GetEducationSchool(ctx context.Context, numberOrID string, queryParam url.Values) (*libregraph.EducationSchool, error) { +func (i *LDAP) GetEducationSchool(ctx context.Context, numberOrID string) (*libregraph.EducationSchool, error) { logger := i.logger.SubloggerWithRequestID(ctx) logger.Debug().Str("backend", "ldap").Msg("GetEducationSchool") e, err := i.getSchoolByNumberOrID(numberOrID) @@ -304,7 +303,7 @@ func (i *LDAP) GetEducationSchool(ctx context.Context, numberOrID string, queryP } // GetEducationSchools implements the EducationBackend interface for the LDAP backend. -func (i *LDAP) GetEducationSchools(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationSchool, error) { +func (i *LDAP) GetEducationSchools(ctx context.Context) ([]*libregraph.EducationSchool, error) { var filter string filter = fmt.Sprintf("(objectClass=%s)", i.educationConfig.schoolObjectClass) diff --git a/services/graph/pkg/identity/ldap_education_school_test.go b/services/graph/pkg/identity/ldap_education_school_test.go index d4865d19fd..47cc36437b 100644 --- a/services/graph/pkg/identity/ldap_education_school_test.go +++ b/services/graph/pkg/identity/ldap_education_school_test.go @@ -264,7 +264,7 @@ func TestGetEducationSchool(t *testing.T) { b, err := getMockedBackend(lm, eduConfig, &logger) assert.Nil(t, err) - school, err := b.GetEducationSchool(context.Background(), tt.numberOrId, nil) + school, err := b.GetEducationSchool(context.Background(), tt.numberOrId) lm.AssertNumberOfCalls(t, "Search", 1) if tt.expectedItemNotFound { @@ -293,7 +293,7 @@ func TestGetEducationSchools(t *testing.T) { // lm.On("Search", sr2).Return(&ldap.SearchResult{Entries: []*ldap.Entry{}}, nil) b, err := getMockedBackend(lm, eduConfig, &logger) assert.Nil(t, err) - _, err = b.GetEducationSchools(context.Background(), nil) + _, err = b.GetEducationSchools(context.Background()) lm.AssertNumberOfCalls(t, "Search", 1) assert.Nil(t, err) } diff --git a/services/graph/pkg/identity/ldap_education_user.go b/services/graph/pkg/identity/ldap_education_user.go index 3df8dffba7..e346bd9897 100644 --- a/services/graph/pkg/identity/ldap_education_user.go +++ b/services/graph/pkg/identity/ldap_education_user.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "net/url" "strings" "github.com/go-ldap/ldap/v3" @@ -82,7 +81,7 @@ func (i *LDAP) UpdateEducationUser(ctx context.Context, nameOrID string, user li } // GetEducationUser implements the EducationBackend interface for the LDAP backend. -func (i *LDAP) GetEducationUser(ctx context.Context, nameOrID string, queryParam url.Values) (*libregraph.EducationUser, error) { +func (i *LDAP) GetEducationUser(ctx context.Context, nameOrID string) (*libregraph.EducationUser, error) { logger := i.logger.SubloggerWithRequestID(ctx) logger.Debug().Str("backend", "ldap").Msg("GetEducationUser") e, err := i.getEducationUserByNameOrID(nameOrID) @@ -97,29 +96,16 @@ func (i *LDAP) GetEducationUser(ctx context.Context, nameOrID string, queryParam } // GetEducationUsers implements the EducationBackend interface for the LDAP backend. -func (i *LDAP) GetEducationUsers(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationUser, error) { +func (i *LDAP) GetEducationUsers(ctx context.Context) ([]*libregraph.EducationUser, error) { logger := i.logger.SubloggerWithRequestID(ctx) logger.Debug().Str("backend", "ldap").Msg("GetEducationUsers") - search := queryParam.Get("search") - if search == "" { - search = queryParam.Get("$search") - } var userFilter string - if search != "" { - search = ldap.EscapeFilter(search) - userFilter = fmt.Sprintf( - "(|(%s=%s*)(%s=%s*)(%s=%s*))", - i.userAttributeMap.userName, search, - i.userAttributeMap.mail, search, - i.userAttributeMap.displayName, search, - ) - } - if userFilter == "" && i.userFilter == "" { + if i.userFilter == "" { userFilter = fmt.Sprintf("(objectClass=%s)", i.educationConfig.userObjectClass) } else { - userFilter = fmt.Sprintf("(&%s(objectClass=%s)%s)", i.userFilter, i.educationConfig.userObjectClass, userFilter) + userFilter = fmt.Sprintf("(&%s(objectClass=%s))", i.userFilter, i.educationConfig.userObjectClass) } searchRequest := ldap.NewSearchRequest( diff --git a/services/graph/pkg/identity/ldap_education_user_test.go b/services/graph/pkg/identity/ldap_education_user_test.go index 156ba29780..960bec73be 100644 --- a/services/graph/pkg/identity/ldap_education_user_test.go +++ b/services/graph/pkg/identity/ldap_education_user_test.go @@ -114,13 +114,13 @@ func TestGetEducationUser(t *testing.T) { lm.On("Search", sr2).Return(&ldap.SearchResult{Entries: []*ldap.Entry{}}, nil) b, err := getMockedBackend(lm, eduConfig, &logger) assert.Nil(t, err) - user, err := b.GetEducationUser(context.Background(), "abcd-defg", nil) + user, err := b.GetEducationUser(context.Background(), "abcd-defg") lm.AssertNumberOfCalls(t, "Search", 1) assert.Nil(t, err) assert.Equal(t, "Test User", user.GetDisplayName()) assert.Equal(t, "abcd-defg", user.GetId()) - _, err = b.GetEducationUser(context.Background(), "xxxx-xxxx", nil) + _, err = b.GetEducationUser(context.Background(), "xxxx-xxxx") lm.AssertNumberOfCalls(t, "Search", 2) assert.NotNil(t, err) assert.Equal(t, "itemNotFound", err.Error()) @@ -139,7 +139,7 @@ func TestGetEducationUsers(t *testing.T) { lm.On("Search", sr).Return(&ldap.SearchResult{Entries: []*ldap.Entry{eduUserEntry}}, nil) b, err := getMockedBackend(lm, eduConfig, &logger) assert.Nil(t, err) - _, err = b.GetEducationUsers(context.Background(), nil) + _, err = b.GetEducationUsers(context.Background()) lm.AssertNumberOfCalls(t, "Search", 1) assert.Nil(t, err) } diff --git a/services/graph/pkg/identity/mocks/education_backend.go b/services/graph/pkg/identity/mocks/education_backend.go index dd6af905e8..42e7cbdbaf 100644 --- a/services/graph/pkg/identity/mocks/education_backend.go +++ b/services/graph/pkg/identity/mocks/education_backend.go @@ -8,8 +8,6 @@ import ( libregraph "github.com/owncloud/libre-graph-api-go" mock "github.com/stretchr/testify/mock" - - url "net/url" ) // EducationBackend is an autogenerated mock type for the EducationBackend type @@ -156,13 +154,13 @@ func (_m *EducationBackend) DeleteEducationUser(ctx context.Context, nameOrID st return r0 } -// GetEducationClass provides a mock function with given fields: ctx, namedOrID, queryParam -func (_m *EducationBackend) GetEducationClass(ctx context.Context, namedOrID string, queryParam url.Values) (*libregraph.EducationClass, error) { - ret := _m.Called(ctx, namedOrID, queryParam) +// GetEducationClass provides a mock function with given fields: ctx, namedOrID +func (_m *EducationBackend) GetEducationClass(ctx context.Context, namedOrID string) (*libregraph.EducationClass, error) { + ret := _m.Called(ctx, namedOrID) var r0 *libregraph.EducationClass - if rf, ok := ret.Get(0).(func(context.Context, string, url.Values) *libregraph.EducationClass); ok { - r0 = rf(ctx, namedOrID, queryParam) + if rf, ok := ret.Get(0).(func(context.Context, string) *libregraph.EducationClass); ok { + r0 = rf(ctx, namedOrID) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*libregraph.EducationClass) @@ -170,8 +168,8 @@ func (_m *EducationBackend) GetEducationClass(ctx context.Context, namedOrID str } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, url.Values) error); ok { - r1 = rf(ctx, namedOrID, queryParam) + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, namedOrID) } else { r1 = ret.Error(1) } @@ -202,13 +200,13 @@ func (_m *EducationBackend) GetEducationClassMembers(ctx context.Context, nameOr return r0, r1 } -// GetEducationClasses provides a mock function with given fields: ctx, queryParam -func (_m *EducationBackend) GetEducationClasses(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationClass, error) { - ret := _m.Called(ctx, queryParam) +// GetEducationClasses provides a mock function with given fields: ctx +func (_m *EducationBackend) GetEducationClasses(ctx context.Context) ([]*libregraph.EducationClass, error) { + ret := _m.Called(ctx) var r0 []*libregraph.EducationClass - if rf, ok := ret.Get(0).(func(context.Context, url.Values) []*libregraph.EducationClass); ok { - r0 = rf(ctx, queryParam) + if rf, ok := ret.Get(0).(func(context.Context) []*libregraph.EducationClass); ok { + r0 = rf(ctx) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]*libregraph.EducationClass) @@ -216,8 +214,8 @@ func (_m *EducationBackend) GetEducationClasses(ctx context.Context, queryParam } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, url.Values) error); ok { - r1 = rf(ctx, queryParam) + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) } else { r1 = ret.Error(1) } @@ -225,13 +223,13 @@ func (_m *EducationBackend) GetEducationClasses(ctx context.Context, queryParam return r0, r1 } -// GetEducationSchool provides a mock function with given fields: ctx, nameOrID, queryParam -func (_m *EducationBackend) GetEducationSchool(ctx context.Context, nameOrID string, queryParam url.Values) (*libregraph.EducationSchool, error) { - ret := _m.Called(ctx, nameOrID, queryParam) +// GetEducationSchool provides a mock function with given fields: ctx, nameOrID +func (_m *EducationBackend) GetEducationSchool(ctx context.Context, nameOrID string) (*libregraph.EducationSchool, error) { + ret := _m.Called(ctx, nameOrID) var r0 *libregraph.EducationSchool - if rf, ok := ret.Get(0).(func(context.Context, string, url.Values) *libregraph.EducationSchool); ok { - r0 = rf(ctx, nameOrID, queryParam) + if rf, ok := ret.Get(0).(func(context.Context, string) *libregraph.EducationSchool); ok { + r0 = rf(ctx, nameOrID) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*libregraph.EducationSchool) @@ -239,8 +237,8 @@ func (_m *EducationBackend) GetEducationSchool(ctx context.Context, nameOrID str } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, url.Values) error); ok { - r1 = rf(ctx, nameOrID, queryParam) + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, nameOrID) } else { r1 = ret.Error(1) } @@ -294,13 +292,13 @@ func (_m *EducationBackend) GetEducationSchoolUsers(ctx context.Context, id stri return r0, r1 } -// GetEducationSchools provides a mock function with given fields: ctx, queryParam -func (_m *EducationBackend) GetEducationSchools(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationSchool, error) { - ret := _m.Called(ctx, queryParam) +// GetEducationSchools provides a mock function with given fields: ctx +func (_m *EducationBackend) GetEducationSchools(ctx context.Context) ([]*libregraph.EducationSchool, error) { + ret := _m.Called(ctx) var r0 []*libregraph.EducationSchool - if rf, ok := ret.Get(0).(func(context.Context, url.Values) []*libregraph.EducationSchool); ok { - r0 = rf(ctx, queryParam) + if rf, ok := ret.Get(0).(func(context.Context) []*libregraph.EducationSchool); ok { + r0 = rf(ctx) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]*libregraph.EducationSchool) @@ -308,8 +306,8 @@ func (_m *EducationBackend) GetEducationSchools(ctx context.Context, queryParam } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, url.Values) error); ok { - r1 = rf(ctx, queryParam) + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) } else { r1 = ret.Error(1) } @@ -317,13 +315,13 @@ func (_m *EducationBackend) GetEducationSchools(ctx context.Context, queryParam return r0, r1 } -// GetEducationUser provides a mock function with given fields: ctx, nameOrID, queryParam -func (_m *EducationBackend) GetEducationUser(ctx context.Context, nameOrID string, queryParam url.Values) (*libregraph.EducationUser, error) { - ret := _m.Called(ctx, nameOrID, queryParam) +// GetEducationUser provides a mock function with given fields: ctx, nameOrID +func (_m *EducationBackend) GetEducationUser(ctx context.Context, nameOrID string) (*libregraph.EducationUser, error) { + ret := _m.Called(ctx, nameOrID) var r0 *libregraph.EducationUser - if rf, ok := ret.Get(0).(func(context.Context, string, url.Values) *libregraph.EducationUser); ok { - r0 = rf(ctx, nameOrID, queryParam) + if rf, ok := ret.Get(0).(func(context.Context, string) *libregraph.EducationUser); ok { + r0 = rf(ctx, nameOrID) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*libregraph.EducationUser) @@ -331,8 +329,8 @@ func (_m *EducationBackend) GetEducationUser(ctx context.Context, nameOrID strin } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, url.Values) error); ok { - r1 = rf(ctx, nameOrID, queryParam) + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, nameOrID) } else { r1 = ret.Error(1) } @@ -340,13 +338,13 @@ func (_m *EducationBackend) GetEducationUser(ctx context.Context, nameOrID strin return r0, r1 } -// GetEducationUsers provides a mock function with given fields: ctx, queryParam -func (_m *EducationBackend) GetEducationUsers(ctx context.Context, queryParam url.Values) ([]*libregraph.EducationUser, error) { - ret := _m.Called(ctx, queryParam) +// GetEducationUsers provides a mock function with given fields: ctx +func (_m *EducationBackend) GetEducationUsers(ctx context.Context) ([]*libregraph.EducationUser, error) { + ret := _m.Called(ctx) var r0 []*libregraph.EducationUser - if rf, ok := ret.Get(0).(func(context.Context, url.Values) []*libregraph.EducationUser); ok { - r0 = rf(ctx, queryParam) + if rf, ok := ret.Get(0).(func(context.Context) []*libregraph.EducationUser); ok { + r0 = rf(ctx) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]*libregraph.EducationUser) @@ -354,8 +352,8 @@ func (_m *EducationBackend) GetEducationUsers(ctx context.Context, queryParam ur } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, url.Values) error); ok { - r1 = rf(ctx, queryParam) + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) } else { r1 = ret.Error(1) } diff --git a/services/graph/pkg/service/v0/educationclasses.go b/services/graph/pkg/service/v0/educationclasses.go index 330a8ef270..989a8be7f6 100644 --- a/services/graph/pkg/service/v0/educationclasses.go +++ b/services/graph/pkg/service/v0/educationclasses.go @@ -32,7 +32,7 @@ func (g Graph) GetEducationClasses(w http.ResponseWriter, r *http.Request) { return } - classes, err := g.identityEducationBackend.GetEducationClasses(r.Context(), r.URL.Query()) + classes, err := g.identityEducationBackend.GetEducationClasses(r.Context()) if err != nil { logger.Debug().Err(err).Msg("could not get classes: backend error") var errcode errorcode.Error @@ -226,7 +226,7 @@ func (g Graph) GetEducationClass(w http.ResponseWriter, r *http.Request) { Str("id", classID). Interface("query", r.URL.Query()). Msg("calling get class on backend") - class, err := g.identityEducationBackend.GetEducationClass(r.Context(), classID, r.URL.Query()) + class, err := g.identityEducationBackend.GetEducationClass(r.Context(), classID) if err != nil { logger.Debug().Err(err).Msg("could not get class: backend error") var errcode errorcode.Error diff --git a/services/graph/pkg/service/v0/educationschools.go b/services/graph/pkg/service/v0/educationschools.go index e54dc0bf6b..6dc797bdeb 100644 --- a/services/graph/pkg/service/v0/educationschools.go +++ b/services/graph/pkg/service/v0/educationschools.go @@ -30,7 +30,7 @@ func (g Graph) GetEducationSchools(w http.ResponseWriter, r *http.Request) { return } - schools, err := g.identityEducationBackend.GetEducationSchools(r.Context(), r.URL.Query()) + schools, err := g.identityEducationBackend.GetEducationSchools(r.Context()) if err != nil { logger.Debug().Err(err).Msg("could not get schools: backend error") var errcode errorcode.Error @@ -170,7 +170,7 @@ func (g Graph) GetEducationSchool(w http.ResponseWriter, r *http.Request) { Str("id", schoolID). Interface("query", r.URL.Query()). Msg("calling get school on backend") - school, err := g.identityEducationBackend.GetEducationSchool(r.Context(), schoolID, r.URL.Query()) + school, err := g.identityEducationBackend.GetEducationSchool(r.Context(), schoolID) if err != nil { logger.Debug().Err(err).Msg("could not get school: backend error") var errcode errorcode.Error diff --git a/services/graph/pkg/service/v0/educationuser.go b/services/graph/pkg/service/v0/educationuser.go index 1ce9dae2a2..dab3d47540 100644 --- a/services/graph/pkg/service/v0/educationuser.go +++ b/services/graph/pkg/service/v0/educationuser.go @@ -10,11 +10,9 @@ import ( "strings" "github.com/CiscoM31/godata" - cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" revactx "github.com/cs3org/reva/v2/pkg/ctx" "github.com/cs3org/reva/v2/pkg/events" - "github.com/cs3org/reva/v2/pkg/rgrpc/status" "github.com/cs3org/reva/v2/pkg/utils" "github.com/go-chi/chi/v5" "github.com/go-chi/render" @@ -22,7 +20,6 @@ import ( settings "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0" "github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode" settingssvc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" - "golang.org/x/exp/slices" ) // GetEducationUsers implements the Service interface. @@ -38,7 +35,7 @@ func (g Graph) GetEducationUsers(w http.ResponseWriter, r *http.Request) { } logger.Debug().Interface("query", r.URL.Query()).Msg("calling get education users on backend") - users, err := g.identityEducationBackend.GetEducationUsers(r.Context(), r.URL.Query()) + users, err := g.identityEducationBackend.GetEducationUsers(r.Context()) if err != nil { logger.Debug().Err(err).Interface("query", r.URL.Query()).Msg("could not get education users from backend") var errcode errorcode.Error @@ -197,7 +194,7 @@ func (g Graph) GetEducationUser(w http.ResponseWriter, r *http.Request) { } logger.Debug().Str("id", userID).Msg("calling get education user from backend") - user, err := g.identityEducationBackend.GetEducationUser(r.Context(), userID, r.URL.Query()) + user, err := g.identityEducationBackend.GetEducationUser(r.Context(), userID) if err != nil { logger.Debug().Err(err).Msg("could not get education user: error fetching education user from backend") @@ -209,65 +206,6 @@ func (g Graph) GetEducationUser(w http.ResponseWriter, r *http.Request) { } return } - sel := strings.Split(r.URL.Query().Get("$select"), ",") - exp := strings.Split(r.URL.Query().Get("$expand"), ",") - if slices.Contains(sel, "drive") || slices.Contains(sel, "drives") || slices.Contains(exp, "drive") || slices.Contains(exp, "drives") { - wdu, err := g.getWebDavBaseURL() - if err != nil { - // log error, wrong configuration - logger.Error(). - Err(err). - Str("webdav_base", g.config.Spaces.WebDavBase). - Str("webdav_path", g.config.Spaces.WebDavPath). - Msg("error parsing webdav URL") - render.Status(r, http.StatusInternalServerError) - return - } - logger.Debug().Str("id", user.GetId()).Msg("calling list storage spaces with education user id filter") - f := listStorageSpacesUserFilter(user.GetId()) - // use the unrestricted flag to get all possible spaces - // users with the canListAllSpaces permission should see all spaces - opaque := utils.AppendPlainToOpaque(nil, "unrestricted", "T") - lspr, err := g.gatewayClient.ListStorageSpaces(r.Context(), &storageprovider.ListStorageSpacesRequest{ - Opaque: opaque, - Filters: []*storageprovider.ListStorageSpacesRequest_Filter{f}, - }) - if err != nil { - // transport error, needs to be fixed by admin - logger.Error().Err(err).Interface("query", r.URL.Query()).Msg("error getting storages: transport error") - render.Status(r, http.StatusInternalServerError) - render.JSON(w, r, user) - return - } - if lspr.GetStatus().GetCode() != cs3rpc.Code_CODE_OK { - logger.Debug().Str("grpc", lspr.GetStatus().GetMessage()).Msg("could not get drive for education user") - // in case of NOT_OK, we can just return the user object with empty drives - render.Status(r, status.HTTPStatusFromCode(http.StatusOK)) - render.JSON(w, r, user) - return - } - drives := []libregraph.Drive{} - for _, sp := range lspr.GetStorageSpaces() { - d, err := g.cs3StorageSpaceToDrive(r.Context(), wdu, sp) - if err != nil { - logger.Debug().Err(err).Interface("id", sp.Id).Msg("error converting space to drive") - continue - } - quota, err := g.getDriveQuota(r.Context(), sp) - if err != nil { - logger.Debug().Err(err).Interface("id", sp.Id).Msg("error calling get quota on drive") - } - d.Quota = "a - if slices.Contains(sel, "drive") || slices.Contains(exp, "drive") { - if *d.DriveType == _spaceTypePersonal { - user.Drive = d - } - } else { - drives = append(drives, *d) - user.Drives = drives - } - } - } render.Status(r, http.StatusOK) render.JSON(w, r, user) @@ -291,7 +229,7 @@ func (g Graph) DeleteEducationUser(w http.ResponseWriter, r *http.Request) { return } logger.Debug().Str("id", userID).Msg("calling get education user on user backend") - user, err := g.identityEducationBackend.GetEducationUser(r.Context(), userID, r.URL.Query()) + user, err := g.identityEducationBackend.GetEducationUser(r.Context(), userID) if err != nil { logger.Debug().Err(err).Str("userID", userID).Msg("failed to get education user from backend") var errcode errorcode.Error diff --git a/services/graph/pkg/service/v0/educationuser_test.go b/services/graph/pkg/service/v0/educationuser_test.go index 1e9e445cbb..ed1c833e11 100644 --- a/services/graph/pkg/service/v0/educationuser_test.go +++ b/services/graph/pkg/service/v0/educationuser_test.go @@ -216,84 +216,8 @@ var _ = Describe("EducationUsers", func() { err = json.Unmarshal(data, &responseUser) Expect(err).ToNot(HaveOccurred()) Expect(responseUser.GetId()).To(Equal("user1")) - Expect(len(responseUser.GetDrives())).To(Equal(0)) }) - It("includes the personal space if requested", func() { - user := &libregraph.EducationUser{} - user.SetId("user1") - - identityEducationBackend.On("GetEducationUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil) - gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{ - Status: status.NewOK(ctx), - TotalBytes: 10, - }, nil) - gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{ - Status: status.NewOK(ctx), - StorageSpaces: []*provider.StorageSpace{ - { - Id: &provider.StorageSpaceId{OpaqueId: "drive1"}, - Root: &provider.ResourceId{SpaceId: "space", OpaqueId: "space"}, - SpaceType: "project", - }, - { - Id: &provider.StorageSpaceId{OpaqueId: "personal"}, - Root: &provider.ResourceId{SpaceId: "personal", OpaqueId: "personal"}, - SpaceType: "personal", - }, - }, - }, nil) - - r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/education/users?$expand=drive", nil) - rctx := chi.NewRouteContext() - rctx.URLParams.Add("userID", *user.Id) - r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) - svc.GetEducationUser(rr, r) - - Expect(rr.Code).To(Equal(http.StatusOK)) - data, err := io.ReadAll(rr.Body) - Expect(err).ToNot(HaveOccurred()) - responseUser := &libregraph.EducationUser{} - err = json.Unmarshal(data, &responseUser) - Expect(err).ToNot(HaveOccurred()) - Expect(responseUser.GetId()).To(Equal("user1")) - Expect(*responseUser.GetDrive().Id).To(Equal("personal")) - }) - - It("includes the drives if requested", func() { - user := &libregraph.EducationUser{} - user.SetId("user1") - - identityEducationBackend.On("GetEducationUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil) - gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{ - Status: status.NewOK(ctx), - TotalBytes: 10, - }, nil) - gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{ - Status: status.NewOK(ctx), - StorageSpaces: []*provider.StorageSpace{ - { - Id: &provider.StorageSpaceId{OpaqueId: "drive1"}, - Root: &provider.ResourceId{SpaceId: "space", OpaqueId: "space"}, - }, - }, - }, nil) - - r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/education/users?$expand=drives", nil) - rctx := chi.NewRouteContext() - rctx.URLParams.Add("userID", *user.Id) - r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) - svc.GetEducationUser(rr, r) - - Expect(rr.Code).To(Equal(http.StatusOK)) - data, err := io.ReadAll(rr.Body) - Expect(err).ToNot(HaveOccurred()) - responseUser := &libregraph.EducationUser{} - err = json.Unmarshal(data, &responseUser) - Expect(err).ToNot(HaveOccurred()) - Expect(responseUser.GetId()).To(Equal("user1")) - Expect(len(responseUser.GetDrives())).To(Equal(1)) - }) }) Describe("PostEducationUser", func() {