Skip to content

Commit 88d69b2

Browse files
medclCopilot
andauthored
feat: support team-based scope for sharing services (#258)
* feat: sharing services handle team related filters * chore: update docs * chore: update terms query util * Update modules/security/share/service.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update modules/security/share/service.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore: add util.mapstr for parameter get utils * chore: fix team filter for document search --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 71b2d4e commit 88d69b2

7 files changed

Lines changed: 245 additions & 70 deletions

File tree

core/orm/query.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,11 @@ func MultiMatchQuery(fields []string, value interface{}) *Clause {
306306
return newLeaf(strings.Join(fields, ","), QueryMultiMatch, value)
307307
}
308308

309-
func TermQuery(field string, value interface{}) *Clause {
309+
func TermQuery[T comparable](field string, value T) *Clause {
310310
return newLeaf(field, QueryTerm, value)
311311
}
312312

313-
// TermsQuery creates a terms query clause from a generic slice
314-
func TermsQuery[T any](field string, value []T) *Clause {
315-
// Convert []T to []interface{}
316-
values := make([]interface{}, len(value))
317-
for i, v := range value {
318-
values[i] = v
319-
}
313+
func TermsQuery[T comparable](field string, values []T) *Clause {
320314
return newLeaf(field, QueryTerms, values)
321315
}
322316

core/param/parameter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,13 @@ func (para *Parameters) GetArray(key ParaKey) ([]interface{}, bool) {
658658
return s, ok
659659
}
660660

661+
if s8, ok := v.(util.MapStr); ok {
662+
for _, v1 := range s8 {
663+
s = append(s, v1)
664+
}
665+
return s, ok
666+
}
667+
661668
//TODO handle rest types
662669
log.Warnf("parameters failed to GetArray, key: %v, type: %v", key, reflect.TypeOf(v))
663670

docs/content.en/docs/release-notes/_index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Information about release notes of INFINI Framework is provided here.
1111
## Latest (In development)
1212
### ❌ Breaking changes
1313
### 🚀 Features
14+
- feat: support team-based scope for sharing services #258
15+
1416
### 🐛 Bug fix
1517
### ✈️ Improvements
1618

modules/security/orm_hooks/hooks.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func init() {
5656
}
5757

5858
resourceType := ctx.MustGetString(orm.SharingResourceType)
59-
per, err := sharingService.GetUserExplicitEffectivePermission(userID, share.NewResourceEntity(resourceType, o1.GetID(), ""))
59+
per, err := sharingService.GetUserExplicitEffectivePermission(sessionUser, share.NewResourceEntity(resourceType, o1.GetID(), ""))
6060
if err == nil {
6161
log.Debug("get permission: ", resourceType, ",", o1.GetID(), " => ", per)
6262
if op == orm.OpGet && per >= 1 {
@@ -65,7 +65,7 @@ func init() {
6565
}
6666

6767
if ctx.GetBool(orm.SharingCategoryCheckingChildrenEnabled, false) {
68-
per, err := sharingService.GetCategoryVisibleWithChildrenSharedObjects(userID, resourceType, o1.GetID())
68+
per, err := sharingService.GetCategoryVisibleWithChildrenSharedObjects(sessionUser, resourceType, o1.GetID())
6969
if err == nil {
7070
if op == orm.OpGet && per >= 1 {
7171
log.Debugf("the resource is category,and children are with %v permission, allow to read access", per)
@@ -142,7 +142,7 @@ func init() {
142142
shareEntity.ResourceParentPath = ctx.MustGetString(orm.SharingResourceParentPath)
143143
}
144144

145-
per, err := sharingService.GetUserExplicitEffectivePermission(userID, shareEntity)
145+
per, err := sharingService.GetUserExplicitEffectivePermission(sessionUser, shareEntity)
146146
if err == nil {
147147
log.Debug("get permission: ", resourceType, ",", o1.GetID(), " => ", per)
148148
if op == orm.OpGet && per >= 1 {
@@ -151,7 +151,7 @@ func init() {
151151
}
152152

153153
if ctx.GetBool(orm.SharingCategoryCheckingChildrenEnabled, false) {
154-
per, err := sharingService.GetCategoryVisibleWithChildrenSharedObjects(userID, resourceType, o1.GetID())
154+
per, err := sharingService.GetCategoryVisibleWithChildrenSharedObjects(sessionUser, resourceType, o1.GetID())
155155
if err == nil {
156156
if op == orm.OpGet && per >= 1 {
157157
log.Debug("the resource is category,and children are with share permission, allow to read access")
@@ -248,7 +248,7 @@ func init() {
248248
shareEntity.ResourceCategoryID = ctx.MustGetString(orm.SharingResourceCategoryID)
249249
shareEntity.ResourceParentPath = ctx.MustGetString(orm.SharingResourceParentPath)
250250
}
251-
per, err := sharingService.GetUserExplicitEffectivePermission(userID, shareEntity)
251+
per, err := sharingService.GetUserExplicitEffectivePermission(sessionUser, shareEntity)
252252
if err == nil {
253253
log.Debug("get permission: ", resourceType, ",", o1.GetID(), " => ", per)
254254
if per >= 4 {
@@ -324,8 +324,8 @@ func init() {
324324

325325
//check if the current user have access to this resource
326326
log.Trace("check if the current user have access to this resource")
327-
perm, err := sharingService.GetUserExplicitEffectivePermission(userID, share.NewResourceEntity(resourceCategoryType, resourceCategoryID, ""))
328-
log.Trace("user have access to this parent object", perm, err)
327+
perm, err := sharingService.GetUserExplicitEffectivePermission(sessionUser, share.NewResourceEntity(resourceCategoryType, resourceCategoryID, ""))
328+
log.Error("user have access to this parent object", perm, err)
329329
if err == nil {
330330
//TODO, not right permission, just 403
331331
//self or not inherit any permission, we should throw a permission error
@@ -337,7 +337,7 @@ func init() {
337337
}
338338
} else {
339339
//for none-documents search
340-
ids, err := sharingService.GetResourceIDsByResourceTypeAndUserID(sessionUser, resourceType)
340+
ids, err := sharingService.GetResourceIDsByResourceTypeForUser(sessionUser, resourceType)
341341
log.Debug("user have access to this parent object", ids, err)
342342
if err == nil {
343343
//TODO, not permission, just 403
@@ -356,7 +356,7 @@ func init() {
356356
//we are search files in specify folder/path
357357
//check if the current user have access to this filtered path
358358
var rules []share.SharingRecord
359-
rules, _ = share.GetSharingRules(security.PrincipalTypeUser, userID, resourceType, "", resourceParentPath, globalShareMustFilters)
359+
rules, _ = share.GetSharingRules(sessionUser, resourceType, "", resourceParentPath, globalShareMustFilters)
360360
log.Trace("get all shared rules: ", resourceParentPath, ",type:", resourceType, " => ", util.MustToJSON(rules))
361361

362362
if len(rules) > 0 {
@@ -428,7 +428,8 @@ func init() {
428428
}
429429
} else {
430430
var rules []share.SharingRecord
431-
rules, _ = share.GetSharingRules(security.PrincipalTypeUser, userID, resourceType, "", resourceParentPath, globalShareMustFilters)
431+
rules, _ = share.GetSharingRules(sessionUser, resourceType, "", resourceParentPath, globalShareMustFilters)
432+
log.Error("user have access to this parent object", util.MustToJSON(rules))
432433
if len(rules) > 0 {
433434
allowedIDs := []string{}
434435
allowedFolderPaths := []string{}
@@ -500,7 +501,7 @@ func init() {
500501
if ctx.GetBool(orm.SharingCategoryCheckingChildrenEnabled, false) {
501502
//eg: get datasource list by find out which doc was shared to you
502503
log.Debug("this is a category, filter out by child shared rules")
503-
vids, _ := sharingService.GetCategoryObjectFromSharedObjects(userID, resourceType)
504+
vids, _ := sharingService.GetCategoryObjectFromSharedObjects(sessionUser, resourceType)
504505
log.Trace("get shared ids via children: ", resourceType, " => ", vids)
505506
if len(vids) > 0 {
506507
bq.ShouldClauses = append(bq.ShouldClauses, orm.TermsQuery("id", vids))

modules/security/share/api.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
package share
66

77
import (
8+
"net/http"
9+
810
"infini.sh/framework/core/api"
911
httprouter "infini.sh/framework/core/api/router"
1012
"infini.sh/framework/core/orm"
1113
"infini.sh/framework/core/security"
12-
"net/http"
1314
)
1415

1516
type APIHandler struct {
@@ -24,12 +25,16 @@ func (h APIHandler) batchGetShares(w http.ResponseWriter, req *http.Request, ps
2425
ctx := orm.NewContextWithParent(req.Context())
2526
service := NewSharingService()
2627

27-
docs, err := service.BatchGetShares(ctx, "", obj)
28+
docs, err := service.BatchGetShares(ctx, nil, obj)
2829
if err != nil {
2930
h.WriteError(w, err.Error(), http.StatusInternalServerError)
3031
return
3132
}
3233

34+
user := security.MustGetUserFromContext(ctx.Context)
35+
36+
docs = service.MergeWithTeamRules(user, docs)
37+
3338
h.WriteJSON(w, docs, 200)
3439
}
3540

modules/security/share/core.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package share
66

77
import (
88
"fmt"
9+
910
"infini.sh/framework/core/orm"
1011
)
1112

@@ -18,11 +19,15 @@ type SharingRecord struct {
1819
ResourceIsFolder bool `json:"resource_is_folder,omitempty" elastic_mapping:"resource_is_folder:{type:boolean}"` //eg: the resource is a folder, means we are sharing a folder
1920
PathPattern string `json:"path_pattern,omitempty" elastic_mapping:"path_pattern:{type:keyword}"` // e.g., "/documents/*" for wildcards
2021
Recursive bool `json:"recursive,omitempty" elastic_mapping:"recursive:{type:boolean}"` // Apply to sub-paths
21-
InheritedFrom string `json:"inherited_from,omitempty" elastic_mapping:"inherited_from:{type:keyword}"` // Parent share ID
22+
InheritedType string `json:"inherited_type,omitempty" elastic_mapping:"inherited_type:{type:keyword}"` // Inherited type
23+
InheritedFrom string `json:"inherited_from,omitempty" elastic_mapping:"inherited_from:{type:keyword}"` // Inherited from ID
2224
InheritedFromFolder string `json:"inherited_from_folder,omitempty" elastic_mapping:"inherited_from_folder:{type:keyword}"` // Parent share ID
2325
Via string `json:"via,omitempty" elastic_mapping:"via:{type:keyword}"` // via: direct / inherit
2426
}
2527

28+
const InheritedTypeTeam = "team"
29+
const InheritedTypeParentFolder = "parent_folder"
30+
2631
const ViaInherit = "inherit"
2732

2833
type SimplifySharingRecord struct {

0 commit comments

Comments
 (0)