Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加一个配置项: 默认资源组, 在用户创建时自动关联; #15

Merged
merged 1 commit into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions common/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
import traceback

import simplejson as json
from django.conf import settings
Expand All @@ -12,7 +13,7 @@
from django.urls import reverse

from common.config import SysConfig
from sql.models import Users
from sql.models import Users, SqlGroup, GroupRelations

logger = logging.getLogger('default')
login_failure_counter = {} # 登录失败锁定计数器,给loginAuthenticate用的
Expand Down Expand Up @@ -92,8 +93,22 @@ def authenticate_entry(request):
group = Group.objects.get(name=default_auth_group)
user.groups.add(group)
except Exception:
logger.error(traceback.format_exc())
logger.error('无name为{}的权限组,无法默认关联,请到系统设置进行配置'.format(default_auth_group))

# 添加到默认资源组
default_resource_group = SysConfig().sys_config.get('default_resource_group', '')
if default_resource_group:
try:
new_relation = GroupRelations(
object_type=0,
object_id = user.id,
object_name = str(user),
group_id = SqlGroup.objects.get(group_name=default_resource_group).group_id,
group_name = default_resource_group)
new_relation.save()
except Exception:
logger.error(traceback.format_exc())
logger.error('无name为{}的资源组,无法默认关联,请到系统设置进行配置'.format(default_resource_group))
# 调用了django内置登录方法,防止管理后台二次登录
user = authenticate(username=username, password=password)
if user:
Expand Down
2 changes: 1 addition & 1 deletion common/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<!-- /.navbar-header -->
<ul class="nav navbar-top-links navbar-right">
<li>
<a>你好,{{ user.display }}</a>
<a>你好,{{ user }}</a>
</li>
{% if todo > 0 %}
<li>
Expand Down
11 changes: 11 additions & 0 deletions common/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ <h4 style="color: darkgrey"><b>其他配置</b></h4>
placeholder="默认权限组名,每次用户登录会自动关联">
</div>
</div>
<div class="form-group">
<label for="default_resource_group"
class="col-sm-4 control-label">DEFAULT_RESOURCE_GROUP</label>
<div class="col-sm-5">
<input type="text" class="form-control"
id="default_resource_group"
key="default_resource_group"
value="{{ config.default_resource_group }}"
placeholder="默认资源组名,每次用户登录会自动关联">
</div>
</div>
<div class="form-group">
<label for="lock_time_threshold"
class="col-sm-4 control-label">LOCK_TIME_THRESHOLD</label>
Expand Down
2 changes: 2 additions & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Users(AbstractUser):
display = models.CharField('显示的中文名', max_length=50, blank=True)

def __str__(self):
if self.display:
return self.display
return self.username

class Meta:
Expand Down