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
7412bdcb
Commit
7412bdcb
authored
Jul 13, 2018
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 完成基本框架
parent
d6ec92d8
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
97 additions
and
29 deletions
+97
-29
asset.py
apps/assets/models/asset.py
+2
-2
mixins.py
apps/common/mixins.py
+1
-1
views.py
apps/jumpserver/views.py
+2
-1
middleware.py
apps/orgs/middleware.py
+2
-1
mixins.py
apps/orgs/mixins.py
+28
-9
models.py
apps/orgs/models.py
+1
-1
views_urls.py
apps/orgs/urls/views_urls.py
+2
-2
views.py
apps/orgs/views.py
+16
-2
_nav.html
apps/templates/_nav.html
+14
-7
api.py
apps/users/api.py
+10
-0
user.py
apps/users/models/user.py
+19
-3
No files found.
apps/assets/models/asset.py
View file @
7412bdcb
...
...
@@ -13,7 +13,7 @@ from django.core.cache import cache
from
..const
import
ASSET_ADMIN_CONN_CACHE_KEY
from
.user
import
AdminUser
,
SystemUser
from
orgs.mixins
import
OrgModelMixin
,
OrgQuerySet
,
OrgManager
from
orgs.mixins
import
OrgModelMixin
,
OrgManager
__all__
=
[
'Asset'
]
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -37,7 +37,7 @@ def default_node():
return
None
class
AssetQuerySet
(
Org
QuerySet
):
class
AssetQuerySet
(
models
.
QuerySet
):
def
active
(
self
):
return
self
.
filter
(
is_active
=
True
)
...
...
apps/common/mixins.py
View file @
7412bdcb
...
...
@@ -120,7 +120,7 @@ class AdminUserRequiredMixin(UserPassesTestMixin):
def
test_func
(
self
):
if
not
self
.
request
.
user
.
is_authenticated
:
return
False
elif
not
self
.
request
.
user
.
is_superuser
:
elif
not
self
.
request
.
user
:
self
.
raise_exception
=
True
return
False
return
True
apps/jumpserver/views.py
View file @
7412bdcb
...
...
@@ -10,9 +10,10 @@ from django.shortcuts import redirect
from
users.models
import
User
from
assets.models
import
Asset
from
terminal.models
import
Session
from
orgs.mixins
import
OrgViewGenericMixin
class
IndexView
(
LoginRequiredMixin
,
TemplateView
):
class
IndexView
(
LoginRequiredMixin
,
OrgViewGenericMixin
,
TemplateView
):
template_name
=
'index.html'
session_week
=
None
...
...
apps/orgs/middleware.py
View file @
7412bdcb
# -*- coding: utf-8 -*-
#
from
.utils
import
get_org_from_request
from
.utils
import
get_org_from_request
,
set_current_org
class
OrgMiddleware
:
...
...
@@ -11,5 +11,6 @@ class OrgMiddleware:
def
__call__
(
self
,
request
):
org
=
get_org_from_request
(
request
)
request
.
current_org
=
org
set_current_org
(
org
)
response
=
self
.
get_response
(
request
)
return
response
apps/orgs/mixins.py
View file @
7412bdcb
# -*- coding: utf-8 -*-
#
from
django.db
import
models
from
django.shortcuts
import
redirect
from
django.contrib.auth
import
get_user_model
from
common.utils
import
get_logger
from
.utils
import
get_current_org
,
get_model_by_db_table
...
...
@@ -8,24 +10,27 @@ from .utils import get_current_org, get_model_by_db_table
logger
=
get_logger
(
__file__
)
class
OrgQuerySet
(
models
.
QuerySet
):
__all__
=
[
'OrgManager'
,
'OrgViewGenericMixin'
,
'OrgModelMixin'
]
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
()
.
__init__
(
*
args
,
**
kwargs
)
class
OrgManager
(
OrgQuerySet
.
as_manager
()
.
__class__
):
class
OrgManager
(
models
.
Manager
):
def
get_queryset
(
self
):
current_org
=
get_current_org
()
user_model
=
get_user_model
()
kwargs
=
{}
print
(
"Get queryset "
)
print
(
self
.
model
)
print
(
current_org
)
if
not
current_org
:
kwargs
[
'id'
]
=
None
elif
current_org
.
is_real
:
elif
issubclass
(
self
.
model
,
user_model
):
kwargs
[
'orgs'
]
=
current_org
elif
current_org
.
is_real
():
kwargs
[
'org'
]
=
current_org
elif
current_org
.
is_default
():
kwargs
[
'org'
]
=
None
print
(
"GET QUWRYSET "
)
print
(
kwargs
)
return
super
()
.
get_queryset
()
.
filter
(
**
kwargs
)
...
...
@@ -55,11 +60,25 @@ class OrgModelMixin(models.Model):
def
save
(
self
,
force_insert
=
False
,
force_update
=
False
,
using
=
None
,
update_fields
=
None
):
user_model
=
get_user_model
()
current_org
=
get_current_org
()
if
current_org
and
not
current_org
.
is_real
():
self
.
org
=
current_org
return
super
()
.
save
(
force_insert
=
force_insert
,
force_update
=
force_update
,
using
=
using
,
update_fields
=
update_fields
)
instance
=
super
()
.
save
(
force_insert
=
force_insert
,
force_update
=
force_update
,
using
=
using
,
update_fields
=
update_fields
)
if
isinstance
(
instance
,
user_model
):
instance
.
orgs
.
add
(
current_org
)
return
instance
class
Meta
:
abstract
=
True
class
OrgViewGenericMixin
:
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
current_org
=
get_current_org
()
if
not
current_org
:
return
redirect
(
'orgs:switch-a-org'
)
return
super
()
.
dispatch
(
request
,
*
args
,
**
kwargs
)
apps/orgs/models.py
View file @
7412bdcb
...
...
@@ -63,7 +63,7 @@ class Organization(models.Model):
pass
def
is_real
(
self
):
return
len
(
str
(
self
.
id
))
==
3
2
return
len
(
str
(
self
.
id
))
==
3
6
@classmethod
def
get_user_admin_orgs
(
cls
,
user
):
...
...
apps/orgs/urls/views_urls.py
View file @
7412bdcb
...
...
@@ -8,6 +8,6 @@ app_name = 'orgs'
urlpatterns
=
[
url
(
r'^(?P<pk>.*)/switch/$'
,
views
.
SwitchOrgView
.
as_view
(),
name
=
'org-switch'
)
url
(
r'^(?P<pk>.*)/switch/$'
,
views
.
SwitchOrgView
.
as_view
(),
name
=
'org-switch'
),
url
(
r'^switch-a-org/$'
,
views
.
SwitchToAOrgView
.
as_view
(),
name
=
'switch-a-org'
)
]
apps/orgs/views.py
View file @
7412bdcb
from
django.shortcuts
import
redirect
from
django.shortcuts
import
redirect
,
reverse
from
django.http
import
HttpResponseForbidden
from
django.views.generic
import
DetailView
from
django.views.generic
import
DetailView
,
View
from
.models
import
Organization
...
...
@@ -14,3 +15,16 @@ class SwitchOrgView(DetailView):
self
.
object
=
Organization
.
get_instance
(
pk
)
request
.
session
[
'oid'
]
=
self
.
object
.
id
.
__str__
()
return
redirect
(
'index'
)
class
SwitchToAOrgView
(
View
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
admin_orgs
=
Organization
.
get_user_admin_orgs
(
request
.
user
)
if
not
admin_orgs
:
return
HttpResponseForbidden
()
default_org
=
Organization
.
default
()
if
default_org
in
admin_orgs
:
redirect_org
=
default_org
else
:
redirect_org
=
admin_orgs
[
0
]
return
redirect
(
reverse
(
'orgs:org-switch'
,
kwargs
=
{
'pk'
:
redirect_org
.
id
}))
apps/templates/_nav.html
View file @
7412bdcb
{% load i18n %}
{% if ADMIN_ORGS %}
{% if ADMIN_ORGS
and ADMIN_ORGS|length > 1
%}
<li
id=
"org"
>
<a
class=
"dropdown-toggle"
data-toggle=
"dropdown"
aria-expanded=
"false"
>
<i
class=
"fa fa-star"
style=
"width: 14px"
></i>
<span
class=
"nav-label"
>
{{ CURRENT_ORG.name }}
</span>
...
...
@@ -7,8 +7,8 @@
</a>
<ul
class=
"dropdown-menu"
>
{% for org in ADMIN_ORGS %}
{% if org.i
s_default
%}
<li><a
class=
"org-dropdown"
href=
"{% url 'orgs:org-switch' pk=org.id %}"
data-id=
"{{ org.id }}"
>
{{ org.name }}
</a></li>
{% if org.i
d != CURRENT_ORG.id
%}
<li><a
class=
"org-dropdown"
href=
"{% url 'orgs:org-switch' pk=org.id %}"
data-id=
"{{ org.id }}"
>
{{ org.name }}
</a></li>
{% endif %}
{% endfor %}
</ul>
...
...
@@ -17,8 +17,8 @@
{% endif %}
<li
id=
"index"
>
<a
href=
"{% url 'index' %}"
>
<i
class=
"fa fa-dashboard"
style=
"width: 14px"
></i>
<span
class=
"nav-label"
>
{% trans 'Dashboard' %}
</span>
<span
class=
"label label-info pull-right"
></span>
<i
class=
"fa fa-dashboard"
style=
"width: 14px"
></i>
<span
class=
"nav-label"
>
{% trans 'Dashboard' %}
</span>
<span
class=
"label label-info pull-right"
></span>
</a>
</li>
<li
id=
"users"
>
...
...
@@ -96,4 +96,11 @@
<a
href=
"{% url 'settings:basic-setting' %}"
>
<i
class=
"fa fa-gears"
></i>
<span
class=
"nav-label"
>
{% trans 'Settings' %}
</span><span
class=
"label label-info pull-right"
></span>
</a>
</li>
\ No newline at end of file
</li>
<script>
$
(
document
).
ready
(
function
()
{
var
current_org
=
'{{ CURRENT_ORG.name }}'
;
console
.
log
(
current_org
);
})
</script>
\ No newline at end of file
apps/users/api.py
View file @
7412bdcb
...
...
@@ -20,6 +20,7 @@ from .permissions import IsSuperUser, IsValidUser, IsCurrentUserOrReadOnly, \
IsSuperUserOrAppUser
from
.utils
import
check_user_valid
,
generate_token
,
get_login_ip
,
\
check_otp_code
,
set_user_login_failed_count_to_cache
,
is_block_login
from
orgs.utils
import
get_current_org
from
common.mixins
import
IDInFilterMixin
from
common.utils
import
get_logger
...
...
@@ -33,6 +34,15 @@ class UserViewSet(IDInFilterMixin, BulkModelViewSet):
permission_classes
=
(
IsSuperUser
,)
filter_fields
=
(
'username'
,
'email'
,
'name'
,
'id'
)
def
get_queryset
(
self
):
queryset
=
super
()
.
get_queryset
()
current_org
=
get_current_org
()
if
current_org
.
is_real
():
queryset
=
queryset
.
filter
(
orgs
=
current_org
)
elif
current_org
.
is_default
():
queryset
=
queryset
.
filter
(
orgs
=
None
)
return
queryset
def
get_permissions
(
self
):
if
self
.
action
==
"retrieve"
:
self
.
permission_classes
=
(
IsSuperUserOrAppUser
,)
...
...
apps/users/models/user.py
View file @
7412bdcb
...
...
@@ -6,7 +6,7 @@ from collections import OrderedDict
from
django.conf
import
settings
from
django.contrib.auth.hashers
import
make_password
from
django.contrib.auth.models
import
AbstractUser
from
django.contrib.auth.models
import
AbstractUser
,
UserManager
from
django.core
import
signing
from
django.db
import
models
from
django.utils.translation
import
ugettext_lazy
as
_
...
...
@@ -15,6 +15,7 @@ from django.shortcuts import reverse
from
common.utils
import
get_signer
,
date_expired_default
from
common.models
import
Setting
from
orgs.utils
import
get_current_org
__all__
=
[
'User'
]
...
...
@@ -186,6 +187,18 @@ class User(AbstractUser):
else
:
self
.
role
=
'User'
@property
def
admin_orgs
(
self
):
from
orgs.models
import
Organization
return
Organization
.
get_user_admin_orgs
(
self
)
@property
def
is_org_admin
(
self
):
if
self
.
is_superuser
or
self
.
admin_orgs
:
return
True
else
:
return
False
@property
def
is_app
(
self
):
return
self
.
role
==
'App'
...
...
@@ -207,8 +220,11 @@ class User(AbstractUser):
if
self
.
username
==
'admin'
:
self
.
role
=
'Admin'
self
.
is_active
=
True
super
()
.
save
(
*
args
,
**
kwargs
)
instance
=
super
()
.
save
(
*
args
,
**
kwargs
)
current_org
=
get_current_org
()
if
current_org
and
current_org
.
is_real
():
instance
.
orgs
.
add
(
current_org
)
return
instance
@property
def
private_token
(
self
):
...
...
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