Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
fad842a8
Commit
fad842a8
authored
Sep 23, 2019
by
BaiJiangJie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 添加资产授权规则 generate_fake 方法
parent
02944d40
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
2 deletions
+53
-2
asset_permission.py
apps/perms/models/asset_permission.py
+53
-2
No files found.
apps/perms/models/asset_permission.py
View file @
fad842a8
import
uuid
import
uuid
import
logging
from
functools
import
reduce
from
functools
import
reduce
from
django.db
import
models
from
django.db
import
models
from
django.db.models
import
Q
from
django.db.models
import
Q
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.translation
import
ugettext_lazy
as
_
from
common.utils
import
date_expired_default
from
orgs.models
import
Organization
from
orgs.
mixins.models
import
OrgModelMixin
from
orgs.
utils
import
get_current_org
from
assets.models
import
Asset
,
SystemUser
,
Node
from
assets.models
import
Asset
,
SystemUser
,
Node
from
.base
import
BasePermission
from
.base
import
BasePermission
...
@@ -15,6 +16,7 @@ from .base import BasePermission
...
@@ -15,6 +16,7 @@ from .base import BasePermission
__all__
=
[
__all__
=
[
'AssetPermission'
,
'Action'
,
'AssetPermission'
,
'Action'
,
]
]
logger
=
logging
.
getLogger
(
__name__
)
class
Action
:
class
Action
:
...
@@ -106,3 +108,52 @@ class AssetPermission(BasePermission):
...
@@ -106,3 +108,52 @@ class AssetPermission(BasePermission):
args
=
reduce
(
lambda
x
,
y
:
x
|
y
,
args
)
args
=
reduce
(
lambda
x
,
y
:
x
|
y
,
args
)
assets
=
Asset
.
objects
.
filter
(
args
)
.
distinct
()
assets
=
Asset
.
objects
.
filter
(
args
)
.
distinct
()
return
assets
return
assets
@classmethod
def
generate_fake
(
cls
,
count
=
100
):
from
..hands
import
User
,
Node
,
SystemUser
import
random
org
=
get_current_org
()
if
not
org
or
not
org
.
is_real
():
Organization
.
default
()
.
change_to
()
nodes
=
list
(
Node
.
objects
.
all
())
assets
=
list
(
Asset
.
objects
.
all
())
system_users
=
list
(
SystemUser
.
objects
.
all
())
users
=
User
.
objects
.
filter
(
username
=
'admin'
)
for
i
in
range
(
count
):
name
=
"fake_perm_to_admin_{}"
.
format
(
str
(
uuid
.
uuid4
())[:
6
])
perm
=
cls
(
name
=
name
)
try
:
perm
.
save
()
perm
.
users
.
set
(
users
)
if
system_users
and
len
(
system_users
)
>
3
:
_system_users
=
random
.
sample
(
system_users
,
3
)
elif
system_users
:
_system_users
=
[
system_users
[
0
]]
else
:
_system_users
=
[]
perm
.
system_users
.
set
(
_system_users
)
if
nodes
and
len
(
nodes
)
>
3
:
_nodes
=
random
.
sample
(
nodes
,
3
)
else
:
_nodes
=
[
Node
.
default_node
()]
perm
.
nodes
.
set
(
_nodes
)
if
assets
and
len
(
assets
)
>
3
:
_assets
=
random
.
sample
(
assets
,
3
)
elif
assets
:
_assets
=
[
assets
[
0
]]
else
:
_assets
=
[]
perm
.
assets
.
set
(
_assets
)
logger
.
debug
(
'Generate fake perm:
%
s'
%
perm
.
name
)
except
Exception
as
e
:
print
(
'Error continue'
)
continue
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment