Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
gjl2004yn
jumpserver
提交
1216f15e
J
jumpserver
项目概览
gjl2004yn
/
jumpserver
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jumpserver
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1216f15e
编写于
3月 16, 2021
作者:
B
Bai
提交者:
baltery
3月 16, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 修复新旧版本对于default_node节点变更冲突的问题(旧版本会将新版本迁移后的default_node节点的key修改为非1)
上级
cc3911d2
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
36 addition
and
123 deletion
+36
-123
apps/assets/apps.py
apps/assets/apps.py
+0
-14
apps/assets/models/node.py
apps/assets/models/node.py
+33
-96
apps/orgs/migrations/0010_auto_20210219_1241.py
apps/orgs/migrations/0010_auto_20210219_1241.py
+1
-1
apps/orgs/models.py
apps/orgs/models.py
+2
-2
apps/perms/const.py
apps/perms/const.py
+0
-10
未找到文件。
apps/assets/apps.py
浏览文件 @
1216f15e
from
__future__
import
unicode_literals
from
django.apps
import
AppConfig
from
django.db.models.signals
import
post_migrate
def
initial_some_nodes
():
from
.models
import
Node
Node
.
initial_some_nodes
()
def
initial_some_nodes_callback
(
sender
,
**
kwargs
):
initial_some_nodes
()
class
AssetsConfig
(
AppConfig
):
...
...
@@ -19,7 +9,3 @@ class AssetsConfig(AppConfig):
def
ready
(
self
):
super
().
ready
()
from
.
import
signals_handler
try
:
initial_some_nodes
()
except
Exception
:
post_migrate
.
connect
(
initial_some_nodes_callback
,
sender
=
self
)
apps/assets/models/node.py
浏览文件 @
1216f15e
...
...
@@ -465,44 +465,6 @@ class SomeNodesMixin:
empty_key
=
'-11'
empty_value
=
_
(
"empty"
)
@
classmethod
def
correct_default_node_if_need
(
cls
):
with
tmp_to_root_org
():
wrong_default_org
=
cls
.
objects
.
filter
(
key
=
'1'
,
value
=
'Default'
).
first
()
if
not
wrong_default_org
:
return
if
wrong_default_org
.
has_children_or_has_assets
():
return
default_org
=
Organization
.
default
()
right_default_org
=
cls
.
objects
.
filter
(
value
=
default_org
.
name
).
first
()
if
not
right_default_org
:
return
if
right_default_org
.
date_create
>
wrong_default_org
.
date_create
:
return
with
atomic
():
logger
.
warn
(
f
'Correct default node: '
f
'old=
{
wrong_default_org
.
value
}
-
{
wrong_default_org
.
key
}
'
f
'new=
{
right_default_org
.
value
}
-
{
right_default_org
.
key
}
'
)
wrong_default_org
.
delete
()
right_default_org
.
key
=
'1'
right_default_org
.
save
()
@
classmethod
def
default_node
(
cls
):
cls
.
correct_default_node_if_need
()
default_org
=
Organization
.
default
()
with
tmp_to_org
(
default_org
):
defaults
=
{
'value'
:
default_org
.
name
}
obj
,
created
=
cls
.
objects
.
get_or_create
(
defaults
=
defaults
,
key
=
cls
.
default_key
,
)
return
obj
def
is_default_node
(
self
):
return
self
.
key
==
self
.
default_key
...
...
@@ -512,33 +474,6 @@ class SomeNodesMixin:
else
:
return
False
@
classmethod
def
get_next_org_root_node_key
(
cls
):
with
tmp_to_org
(
Organization
.
root
()):
org_nodes_roots
=
cls
.
objects
.
filter
(
key__regex
=
r
'^[0-9]+$'
)
org_nodes_roots_keys
=
org_nodes_roots
.
values_list
(
'key'
,
flat
=
True
)
if
not
org_nodes_roots_keys
:
org_nodes_roots_keys
=
[
'1'
]
max_key
=
max
([
int
(
k
)
for
k
in
org_nodes_roots_keys
])
key
=
str
(
max_key
+
1
)
if
max_key
>
0
else
'2'
return
key
@
classmethod
def
create_org_root_node
(
cls
):
ori_org
=
get_current_org
()
with
transaction
.
atomic
():
key
=
cls
.
get_next_org_root_node_key
()
root
=
cls
.
objects
.
create
(
key
=
key
,
value
=
ori_org
.
name
)
return
root
@
classmethod
def
org_root_nodes
(
cls
):
nodes
=
cls
.
objects
.
filter
(
parent_key
=
''
)
\
.
filter
(
key__regex
=
r
'^[0-9]+$'
)
\
.
exclude
(
key__startswith
=
'-'
)
\
.
order_by
(
'key'
)
return
nodes
@
classmethod
def
org_root
(
cls
):
# 如果使用current_org 在set_current_org时会死循环
...
...
@@ -546,6 +481,7 @@ class SomeNodesMixin:
if
ori_org
and
ori_org
.
is_default
():
return
cls
.
default_node
()
if
ori_org
and
ori_org
.
is_root
():
return
None
...
...
@@ -553,46 +489,47 @@ class SomeNodesMixin:
org_roots_length
=
len
(
org_roots
)
if
org_roots_length
==
1
:
return
org_roots
[
0
]
root
=
org_roots
[
0
]
return
root
elif
org_roots_length
==
0
:
root
=
cls
.
create_org_root_node
()
return
root
else
:
raise
ValueError
(
'Current org root node not 1, get {}'
.
format
(
org_roots_length
))
error
=
'Current org {} root node not 1, get {}'
.
format
(
ori_org
,
org_roots_length
)
raise
ValueError
(
error
)
@
classmethod
def
initial_some_nodes
(
cls
):
cls
.
default_node
()
def
default_node
(
cls
):
default_org
=
Organization
.
default
()
with
tmp_to_org
(
default_org
):
defaults
=
{
'value'
:
default_org
.
name
}
obj
,
created
=
cls
.
objects
.
get_or_create
(
defaults
=
defaults
,
key
=
cls
.
default_key
)
return
obj
@
classmethod
def
modify_other_org_root_node_key
(
cls
):
"""
解决创建 default 节点失败的问题,
因为在其他组织下存在 default 节点,故在 DEFAULT 组织下 get 不到 create 失败
"""
logger
.
info
(
"Modify other org root node key"
)
with
tmp_to_org
(
Organization
.
root
()):
node_key1
=
cls
.
objects
.
filter
(
key
=
'1'
).
first
()
if
not
node_key1
:
logger
.
info
(
"Not found node that `key` = 1"
)
return
if
node_key1
.
org_id
==
''
:
node_key1
.
org_id
=
str
(
Organization
.
default
().
id
)
node_key1
.
save
()
return
def
create_org_root_node
(
cls
):
ori_org
=
get_current_org
()
with
transaction
.
atomic
():
with
tmp_to_org
(
node_key1
.
org
):
org_root_node_new_key
=
cls
.
get_next_org_root_node_key
()
for
n
in
cls
.
objects
.
all
():
old_key
=
n
.
key
key_list
=
n
.
key
.
split
(
':'
)
key_list
[
0
]
=
org_root_node_new_key
new_key
=
':'
.
join
(
key_list
)
n
.
key
=
new_key
n
.
save
()
logger
.
info
(
'Modify key ( {} > {} )'
.
format
(
old_key
,
new_key
))
key
=
cls
.
get_next_org_root_node_key
()
root
=
cls
.
objects
.
create
(
key
=
key
,
value
=
ori_org
.
name
)
return
root
@
classmethod
def
get_next_org_root_node_key
(
cls
):
with
tmp_to_root_org
():
org_nodes_roots
=
cls
.
org_root_nodes
()
org_nodes_roots_keys
=
org_nodes_roots
.
values_list
(
'key'
,
flat
=
True
)
if
not
org_nodes_roots_keys
:
org_nodes_roots_keys
=
[
'1'
]
max_key
=
max
([
int
(
k
)
for
k
in
org_nodes_roots_keys
])
key
=
str
(
max_key
+
1
)
if
max_key
>
0
else
'2'
return
key
@
classmethod
def
org_root_nodes
(
cls
):
root_nodes
=
cls
.
objects
.
filter
(
parent_key
=
''
,
key__regex
=
r
'^[0-9]+$'
)
\
.
exclude
(
key__startswith
=
'-'
).
order_by
(
'key'
)
return
root_nodes
class
Node
(
OrgModelMixin
,
SomeNodesMixin
,
FamilyMixin
,
NodeAssetsMixin
):
...
...
apps/orgs/migrations/0010_auto_20210219_1241.py
浏览文件 @
1216f15e
...
...
@@ -6,7 +6,7 @@ import sys
from
django.db
import
migrations
default_id
=
'00000000-0000-0000-0000-00000000000
1
'
default_id
=
'00000000-0000-0000-0000-00000000000
2
'
def
add_default_org
(
apps
,
schema_editor
):
...
...
apps/orgs/models.py
浏览文件 @
1216f15e
...
...
@@ -28,8 +28,8 @@ class Organization(models.Model):
ROOT_ID
=
'00000000-0000-0000-0000-000000000000'
ROOT_NAME
=
_
(
'GLOBAL'
)
DEFAULT_ID
=
'00000000-0000-0000-0000-00000000000
1
'
DEFAULT_NAME
=
'D
EFAULT
'
DEFAULT_ID
=
'00000000-0000-0000-0000-00000000000
2
'
DEFAULT_NAME
=
'D
efault
'
orgs_mapping
=
None
class
Meta
:
...
...
apps/perms/const.py
已删除
100644 → 0
浏览文件 @
cc3911d2
# -*- coding: utf-8 -*-
#
from
django.utils.translation
import
ugettext_lazy
as
_
UNGROUPED_NODE_ID
=
"00000000-0000-0000-0000-000000000002"
UNGROUPED_NODE_KEY
=
'-2'
UNGROUPED_NODE_VALUE
=
_
(
"Ungrouped"
)
EMPTY_NODE_ID
=
"00000000-0000-0000-0000-000000000003"
EMPTY_NODE_KEY
=
"-3"
EMPTY_NODE_VALUE
=
_
(
"Empty"
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录