def _create_admin(self): user = get_object(User, username=self.admin_user) if user: user.delete() db_add_user(username=self.admin_user, password=self.admin_pass, role='SU', name='admin', groups='', admin_groups='', email='*****@*****.**', uuid='MayBeYouAreTheFirstUser', is_active=True) os.system('id %s &> /dev/null || useradd %s' % (self.admin_user, self.admin_user))
def _create_admin(self): user = get_object(User, username=self.admin_user) if user: user.delete() db_add_user(username=self.admin_user, password=self.admin_pass, role='SU', name='admin', groups='', admin_groups='', email='*****@*****.**', uuid='first', is_active=True) cmd = 'id %s 2> /dev/null 1> /dev/null || useradd %s' % (self.admin_user, self.admin_user) shlex.os.system(cmd)
def _create_admin(self): user = get_object(User, username=self.admin_user) if user: user.delete() db_add_user(username=self.admin_user, password=self.admin_pass, role='SU', name='admin', groups='', admin_groups='', email='*****@*****.**', uuid='first', is_active=True) cmd = 'id %s 2> /dev/null 1> /dev/null || useradd %s' % ( self.admin_user, self.admin_user) shlex.os.system(cmd)
def Register(request): if request.method == 'POST': username = request.POST.get('username', '') name = request.POST.get('name', '') email = request.POST.get('email', '') password = PyCrypt.gen_rand_pass(16) # name = '张雯豪' # email = '*****@*****.**' groups = [] admin_groups = [] role = 'CU' uuid_r = uuid.uuid4().get_hex() ssh_key_pwd = PyCrypt.gen_rand_pass(16) is_active = True send_mail_need = True # extra = request.POST.getlist('extra', []) # is_active = False if '0' in extra else True # send_mail_need = True if '1' in extra else False user = db_add_user(username=username, name=name, password=password, email=email, role=role, uuid=uuid_r, groups=groups, admin_groups=admin_groups, ssh_key_pwd=ssh_key_pwd, is_active=is_active, date_joined=datetime.datetime.now()) server_add_user(username=username, ssh_key_pwd=ssh_key_pwd) user_add_mail(user, kwargs=locals()) return render_to_response('register.html')
def user_add(request): error = '' msg = '' header_title, path1, path2 = '添加用户', '用户管理', '添加用户' user_role = {'SU': '超级管理员', 'CU': '普通用户'} group_all = UserGroup.objects.all() if request.method == 'POST': username = request.POST.get('username', '') password = PyCrypt.gen_rand_pass(16) name = request.POST.get('name', '') email = request.POST.get('email', '') groups = request.POST.getlist('groups', []) admin_groups = request.POST.getlist('admin_groups', []) role = request.POST.get('role', 'CU') uuid_r = uuid.uuid4().hex ssh_key_pwd = PyCrypt.gen_rand_pass(16) extra = request.POST.getlist('extra', []) is_active = False if '0' in extra else True send_mail_need = True if '1' in extra else False try: if '' in [username, password, ssh_key_pwd, name, role]: error = '带*内容不能为空' raise ServerError check_user_is_exist = User.objects.filter(username=username) if check_user_is_exist: error = '用户 %s 已存在' % username raise ServerError if username in ['root']: error = '用户不能为root' raise ServerError except ServerError: pass else: try: if not re.match(r"^\w+$", username): error = '用户名不合法' raise ServerError(error) user = db_add_user(username=username, name=name, password=password, email=email, role=role, uuid=uuid_r, groups=groups, admin_groups=admin_groups, ssh_key_pwd=ssh_key_pwd, is_active=is_active, date_joined=datetime.datetime.now()) server_add_user(username=username, ssh_key_pwd=ssh_key_pwd) user = get_object(User, username=username) if groups: user_groups = [] for user_group_id in groups: user_groups.extend( UserGroup.objects.filter(id=user_group_id)) except IndexError as e: error = '添加用户 %s 失败 %s ' % (username, e) try: db_del_user(username) server_del_user(username) except Exception: pass else: if settings.MAIL_ENABLE and send_mail_need: user_add_mail(user, kwargs=locals()) msg = get_display_msg(user, password=password, ssh_key_pwd=ssh_key_pwd, send_mail_need=send_mail_need) return my_render('juser/user_add.html', locals(), request)