Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
7139be18
Commit
7139be18
authored
2 years ago
by
Anton Sattarov
Browse files
Options
Downloads
Patches
Plain Diff
fix
parent
d0ac1dd4
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
pkg/items/expr.go
+0
-76
0 additions, 76 deletions
pkg/items/expr.go
pkg/items/expr_test.go
+0
-159
0 additions, 159 deletions
pkg/items/expr_test.go
pkg/items/item.go
+8
-0
8 additions, 0 deletions
pkg/items/item.go
pkg/items/util.go
+0
-9
0 additions, 9 deletions
pkg/items/util.go
with
8 additions
and
244 deletions
pkg/items/expr.go
deleted
100644 → 0
+
0
−
76
View file @
d0ac1dd4
package
items
import
(
"context"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/expr"
.
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/conf"
)
const
EnvPerxisKey
=
"$perxis"
type
PerxisExpr
struct
{
ctx
context
.
Context
}
func
(
p
PerxisExpr
)
Fetch
(
i
interface
{})
interface
{}
{
param
,
_
:=
i
.
(
string
)
c
:=
GetContext
(
p
.
ctx
)
switch
param
{
case
"SpaceID"
:
return
c
.
SpaceID
case
"EnvironmentID"
:
return
c
.
EnvID
case
"CollectionID"
:
return
c
.
CollectionID
case
"ItemID"
:
return
c
.
ItemID
case
"Item"
:
return
c
.
Item
case
"Space"
:
return
c
.
Space
case
"Environment"
:
return
c
.
Environment
case
"ViewSpaceID"
:
return
c
.
ViewSpaceID
case
"ViewEnvironmentID"
:
return
c
.
ViewEnvironmentID
case
"ViewCollectionID"
:
return
c
.
ViewCollectionID
case
"ViewSpace"
:
return
c
.
ViewSpace
case
"ViewEnvironment"
:
return
c
.
ViewEnvironment
case
"Principal"
:
return
auth
.
GetPrincipal
(
p
.
ctx
)
case
"PrincipalID"
:
return
auth
.
GetPrincipal
(
p
.
ctx
)
.
GetID
(
p
.
ctx
)
case
"Clients"
:
return
c
.
Clients
default
:
panic
(
"unknown parameter"
)
}
}
func
perxisOption
()
Option
{
return
func
(
c
*
conf
.
Config
)
{
env
,
_
:=
c
.
Env
.
(
map
[
string
]
interface
{})
if
env
!=
nil
{
if
ctx
,
ok
:=
env
[
expr
.
EnvContextKey
]
.
(
context
.
Context
);
ok
{
env
[
EnvPerxisKey
]
=
&
PerxisExpr
{
ctx
:
ctx
}
}
}
}
}
func
init
()
{
expr
.
RegisterOption
(
perxisOption
())
}
This diff is collapsed.
Click to expand it.
pkg/items/expr_test.go
deleted
100644 → 0
+
0
−
159
View file @
d0ac1dd4
package
items
import
(
"context"
"testing"
"git.perx.ru/perxis/perxis-go/pkg/auth"
mockscollaborators
"git.perx.ru/perxis/perxis-go/pkg/collaborators/mocks"
"git.perx.ru/perxis/perxis-go/pkg/expr"
"git.perx.ru/perxis/perxis-go/pkg/members"
mocksmembers
"git.perx.ru/perxis/perxis-go/pkg/members/mocks"
"git.perx.ru/perxis/perxis-go/pkg/roles"
mocksroles
"git.perx.ru/perxis/perxis-go/pkg/roles/mocks"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
mocksSpaces
"git.perx.ru/perxis/perxis-go/pkg/spaces/mocks"
"git.perx.ru/perxis/perxis-go/pkg/users"
mocksusers
"git.perx.ru/perxis/perxis-go/pkg/users/mocks"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func
TestPerxisExpr_Principal
(
t
*
testing
.
T
)
{
var
tt
=
[]
struct
{
name
string
userscall
func
(
usersservice
*
mocksusers
.
Users
)
*
mock
.
Call
memberscall
func
(
membersservice
*
mocksmembers
.
Members
)
*
mock
.
Call
spacescall
func
(
spacesservice
*
mocksSpaces
.
Spaces
)
*
mock
.
Call
rolescall
func
(
rolesservice
*
mocksroles
.
Roles
)
*
mock
.
Call
collaboratorscall
func
(
collaboratorsservice
*
mockscollaborators
.
Collaborators
)
*
mock
.
Call
formula
string
wantErr
bool
expected
interface
{}
}{
{
name
:
"Get current ID"
,
userscall
:
func
(
usersservice
*
mocksusers
.
Users
)
*
mock
.
Call
{
return
usersservice
.
On
(
"GetByIdentity"
,
mock
.
Anything
,
"user_identity"
)
.
Return
(
&
users
.
User
{
ID
:
"user_id"
,
Name
:
"test user"
},
nil
)
.
Once
()
},
formula
:
"$perxis.Principal.GetID(ctx)"
,
wantErr
:
false
,
expected
:
"user_id"
,
},
{
name
:
"Get current ID / User not exist"
,
userscall
:
func
(
usersservice
*
mocksusers
.
Users
)
*
mock
.
Call
{
return
usersservice
.
On
(
"GetByIdentity"
,
mock
.
Anything
,
"user_identity"
)
.
Return
(
nil
,
errors
.
New
(
"not found"
))
.
Once
()
},
formula
:
"$perxis.Principal.GetID(ctx)"
,
wantErr
:
false
,
expected
:
""
,
},
{
name
:
"Get role in org"
,
userscall
:
func
(
usersservice
*
mocksusers
.
Users
)
*
mock
.
Call
{
return
usersservice
.
On
(
"GetByIdentity"
,
mock
.
Anything
,
"user_identity"
)
.
Return
(
&
users
.
User
{
ID
:
"user_id"
,
Name
:
"test user"
},
nil
)
.
Once
()
},
spacescall
:
func
(
spacesservice
*
mocksSpaces
.
Spaces
)
*
mock
.
Call
{
return
spacesservice
.
On
(
"Get"
,
mock
.
Anything
,
"sp"
)
.
Return
(
&
spaces
.
Space
{
ID
:
"sp"
,
OrgID
:
"org"
},
nil
)
.
Once
()
},
memberscall
:
func
(
membersservice
*
mocksmembers
.
Members
)
*
mock
.
Call
{
return
membersservice
.
On
(
"Get"
,
mock
.
Anything
,
"org"
,
"user_id"
)
.
Return
(
members
.
RoleAdmin
,
nil
)
.
Once
()
},
formula
:
"$perxis.Principal.Space('sp').Member(ctx)"
,
wantErr
:
false
,
expected
:
members
.
RoleAdmin
,
},
{
name
:
"Check has space access"
,
userscall
:
func
(
usersservice
*
mocksusers
.
Users
)
*
mock
.
Call
{
return
usersservice
.
On
(
"GetByIdentity"
,
mock
.
Anything
,
"user_identity"
)
.
Return
(
&
users
.
User
{
ID
:
"user_id"
,
Name
:
"test user"
},
nil
)
.
Once
()
},
collaboratorscall
:
func
(
collaboratorsservice
*
mockscollaborators
.
Collaborators
)
*
mock
.
Call
{
return
collaboratorsservice
.
On
(
"Get"
,
mock
.
Anything
,
"sp"
,
"user_id"
)
.
Return
(
"editor"
,
nil
)
.
Once
()
},
formula
:
"$perxis.Principal.Space('sp').HasSpaceAccess(ctx, 'sp')"
,
wantErr
:
false
,
expected
:
true
,
},
{
name
:
"Check has space access for any authorized"
,
userscall
:
func
(
usersservice
*
mocksusers
.
Users
)
*
mock
.
Call
{
return
usersservice
.
On
(
"GetByIdentity"
,
mock
.
Anything
,
"user_identity"
)
.
Return
(
&
users
.
User
{
ID
:
"user_id"
,
Name
:
"test user"
},
nil
)
.
Once
()
},
//spacescall: func(spacesservice *mocksSpaces.Spaces) *mock.Call {
// return spacesservice.On("Get", mock.Anything, "sp").Return(&spaces.Space{ID: "sp", OrgID: "org"}, nil).Once()
//},
collaboratorscall
:
func
(
collaboratorsservice
*
mockscollaborators
.
Collaborators
)
*
mock
.
Call
{
return
collaboratorsservice
.
On
(
"Get"
,
mock
.
Anything
,
"sp"
,
"user_id"
)
.
Return
(
""
,
errors
.
New
(
"not found"
))
.
Once
()
},
rolescall
:
func
(
rolesservice
*
mocksroles
.
Roles
)
*
mock
.
Call
{
return
rolesservice
.
On
(
"Get"
,
mock
.
Anything
,
"sp"
,
roles
.
AuthorizedRole
)
.
Return
(
&
roles
.
Role
{
ID
:
roles
.
AuthorizedRole
},
nil
)
.
Once
()
},
formula
:
"$perxis.Principal.Space('sp').HasSpaceAccess(ctx, 'sp')"
,
wantErr
:
false
,
expected
:
true
,
},
}
for
_
,
v
:=
range
tt
{
t
.
Run
(
v
.
name
,
func
(
t
*
testing
.
T
)
{
usersservice
:=
&
mocksusers
.
Users
{}
spacesservice
:=
&
mocksSpaces
.
Spaces
{}
membersservice
:=
&
mocksmembers
.
Members
{}
collaboratorsservice
:=
&
mockscollaborators
.
Collaborators
{}
rolesservice
:=
&
mocksroles
.
Roles
{}
if
v
.
userscall
!=
nil
{
v
.
userscall
(
usersservice
)
}
if
v
.
spacescall
!=
nil
{
v
.
spacescall
(
spacesservice
)
}
if
v
.
memberscall
!=
nil
{
v
.
memberscall
(
membersservice
)
}
if
v
.
collaboratorscall
!=
nil
{
v
.
collaboratorscall
(
collaboratorsservice
)
}
if
v
.
rolescall
!=
nil
{
v
.
rolescall
(
rolesservice
)
}
factory
:=
&
auth
.
PrincipalFactory
{
Users
:
usersservice
,
Spaces
:
spacesservice
,
Members
:
membersservice
,
Collaborators
:
collaboratorsservice
,
Roles
:
rolesservice
,
}
ctx
:=
auth
.
WithPrincipal
(
context
.
Background
(),
factory
.
Principal
(
"user_identity"
))
res
,
err
:=
expr
.
Eval
(
ctx
,
v
.
formula
,
map
[
string
]
interface
{}{
"ctx"
:
context
.
Background
()})
if
!
v
.
wantErr
{
require
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
v
.
expected
,
res
)
}
else
{
require
.
Error
(
t
,
err
)
}
usersservice
.
AssertExpectations
(
t
)
spacesservice
.
AssertExpectations
(
t
)
membersservice
.
AssertExpectations
(
t
)
collaboratorsservice
.
AssertExpectations
(
t
)
rolesservice
.
AssertExpectations
(
t
)
})
}
}
This diff is collapsed.
Click to expand it.
pkg/items/item.go
+
8
−
0
View file @
7139be18
...
...
@@ -556,3 +556,11 @@ func ItemFromProto(protoItem *pb.Item) *Item {
return
item
}
func
GetItemIDs
(
arr
[]
*
Item
)
[]
string
{
res
:=
make
([]
string
,
len
(
arr
))
for
i
,
e
:=
range
arr
{
res
[
i
]
=
e
.
ID
}
return
res
}
This diff is collapsed.
Click to expand it.
pkg/items/util.go
deleted
100644 → 0
+
0
−
9
View file @
d0ac1dd4
package
items
func
GetItemIDs
(
arr
[]
*
Item
)
[]
string
{
res
:=
make
([]
string
,
len
(
arr
))
for
i
,
e
:=
range
arr
{
res
[
i
]
=
e
.
ID
}
return
res
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment