调整模型

pull/530/head
Administrator 2016-11-27 11:30:23 +08:00
parent 2494fa5846
commit 8e0afb2cc4
5 changed files with 29 additions and 16 deletions

View File

@ -1,12 +1,12 @@
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from ops.api import views as ops_api_view from ops.api import views as ops_api_view
router = DefaultRouter() api_router = DefaultRouter()
router.register(r'host_alia', ops_api_view.HostAliaViewSet) api_router.register(r'host_alia', ops_api_view.HostAliaViewSet)
router.register(r'user_alia', ops_api_view.UserAliaViewSet) api_router.register(r'user_alia', ops_api_view.UserAliaViewSet)
router.register(r'cmd_alia', ops_api_view.CmdAliaViewSet) api_router.register(r'cmd_alia', ops_api_view.CmdAliaViewSet)
router.register(r'runas_alia', ops_api_view.RunasAliaViewSet) api_router.register(r'runas_alia', ops_api_view.RunasAliaViewSet)
router.register(r'extra_conf', ops_api_view.ExtraconfViewSet) api_router.register(r'extra_conf', ops_api_view.ExtraconfViewSet)
router.register(r'privilege', ops_api_view.PrivilegeViewSet) api_router.register(r'privilege', ops_api_view.PrivilegeViewSet)
router.register(r'sudo', ops_api_view.SudoViewSet) api_router.register(r'sudo', ops_api_view.SudoViewSet)
router.register(r'cron', ops_api_view.CronTableViewSet) api_router.register(r'cron', ops_api_view.CronTableViewSet)

View File

@ -20,7 +20,7 @@ from django.conf.urls import url, include
from django.conf import settings from django.conf import settings
from django.conf.urls.static import static from django.conf.urls.static import static
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from jumpserver.api_router import router from jumpserver.api_router import api_router
urlpatterns = [ urlpatterns = [
@ -41,7 +41,7 @@ urlpatterns = [
urlpatterns += [ urlpatterns += [
url(r'^api/v1/ops', include(router.urls)), url(r'^api/v1/ops', include(api_router.urls)),
url(r'^ops/', include('ops.urls', namespace='ops')), url(r'^ops/', include('ops.urls', namespace='ops')),
] ]

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals, absolute_import
from jinja2 import Template from jinja2 import Template
from django.db import models from django.db import models
from assets.models import Asset from assets.models import Asset, AssetGroup
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -40,11 +40,13 @@ class RunasAlia(models.Model):
class Privilege(models.Model): class Privilege(models.Model):
name = models.CharField(max_length=128, unique=True, verbose_name=_('Name'))
user = models.ForeignKey(UserAlia, blank=True, null=True, related_name='privileges') user = models.ForeignKey(UserAlia, blank=True, null=True, related_name='privileges')
host = models.ForeignKey(HostAlia, blank=True, null=True, related_name='privileges') host = models.ForeignKey(HostAlia, blank=True, null=True, related_name='privileges')
runas = models.ForeignKey(RunasAlia, blank=True, null=True, related_name='privileges') runas = models.ForeignKey(RunasAlia, blank=True, null=True, related_name='privileges')
command = models.ForeignKey(CmdAlia, blank=True, null=True, related_name='privileges') command = models.ForeignKey(CmdAlia, blank=True, null=True, related_name='privileges')
nopassword = models.BooleanField(default=True, verbose_name=_('Is_NoPassword')) nopassword = models.BooleanField(default=True, verbose_name=_('Is_NoPassword'))
comment = models.TextField(blank=True, null=True, verbose_name=_('Comment'))
def __unicode__(self): def __unicode__(self):
return "[%s %s %s %s %s]" % (self.user.name, return "[%s %s %s %s %s]" % (self.user.name,
@ -58,7 +60,8 @@ class Privilege(models.Model):
class Extra_conf(models.Model): class Extra_conf(models.Model):
line = models.TextField(blank=True, null=True, verbose_name=_('Extra_Item')) line = models.TextField(blank=True, null=True, verbose_name=_('Extra_Item'),
help_text=_('The extra sudo config line.'))
def __unicode__(self): def __unicode__(self):
return self.line return self.line
@ -72,10 +75,20 @@ class Sudo(models.Model):
:param privileges: <list> [(user, host, runas, command, nopassword),] :param privileges: <list> [(user, host, runas, command, nopassword),]
""" """
asset = models.ForeignKey(Asset, null=True, blank=True, related_name='sudos') assets = models.ManyToManyField(Asset, blank=True, related_name='sudos')
asset_groups = models.ManyToManyField(AssetGroup, blank=True, related_name='sudos')
extra_lines = models.ManyToManyField(Extra_conf, related_name='sudos', blank=True) extra_lines = models.ManyToManyField(Extra_conf, related_name='sudos', blank=True)
privilege_items = models.ManyToManyField(Privilege, related_name='sudos', blank=True) privilege_items = models.ManyToManyField(Privilege, related_name='sudos', blank=True)
@property
def all_assets(self):
assets = list(self.assets.all())
for group in self.asset_groups.all():
for asset in group.assets.all():
if asset not in assets:
assets.append(asset)
return assets
@property @property
def users(self): def users(self):
return {privilege.user.name: privilege.user.user_items.split(',') for privilege in self.privilege_items.all()} return {privilege.user.name: privilege.user.user_items.split(',') for privilege in self.privilege_items.all()}

View File

@ -53,6 +53,6 @@ class CronUpdateView(AdminUserRequiredMixin, UpdateView):
class CronDetailView(DetailView): class CronDetailView(DetailView):
model = CronTable model = CronTable
context_object_name = 'sudo' context_object_name = 'cron'
template_name = 'cron/detail.html' template_name = 'cron/detail.html'

View File

@ -43,7 +43,7 @@
<li id="ops"> <li id="ops">
<a> <a>
<i class="fa fa-inbox"></i> <span class="nav-label">{% trans 'Job Center' %}</span><span class="fa arrow"></span> <i class="fa fa-coffee"></i> <span class="nav-label">{% trans 'Job Center' %}</span><span class="fa arrow"></span>
</a> </a>
<ul class="nav nav-second-level"> <ul class="nav nav-second-level">
<li id="sudo"><a href="{% url 'ops:page-sudo-list' %}">{% trans 'Sudo' %}</a></li> <li id="sudo"><a href="{% url 'ops:page-sudo-list' %}">{% trans 'Sudo' %}</a></li>