def create_user(type): import getpass from app.models import AdminUser from app.utils import md5 if type not in ["kefu", "admin"]: print "fail, the right command should like 'python manage.py create_user (kefu or admin)'" return username = raw_input("用户名:") try: u = AdminUser.objects.get(username=username) print "已存在用户, 创建失败" return except AdminUser.DoesNotExist: pass pwd1 = getpass.getpass('密码: ') pwd2 = getpass.getpass('确认密码: ') if pwd1 != pwd2: print "两次输入密码不一致, 创建用户失败" return u = AdminUser(username=username, password=md5(pwd1)) if type == "kefu": u.is_switch = 0 elif type == "admin": u.is_switch = 0 u.save() print "创建用户成功"
def verify_password(username_or_token, password): try: user = AdminUser.objects(username=username_or_token).first() if user.password == md5(password): # 存储g.user # g.user = user.username return True return False except: return False
def test_admin_incorrect_username_login(self): u = AdminUser(username="******", password="******") db.session.add(u) db.session.commit() response = self.app.post('/admin/login', data=dict(username="******", password="******"), follow_redirects=True) self.assertIn(b'Invalid username or password', response.data)
def test_admin_correct_login(self): u = AdminUser(username="******", password="******") db.session.add(u) db.session.commit() response = self.app.post('/admin/login', data=dict(username="******", password="******"), follow_redirects=True) self.assertIn(b'Carly A. Brock', response.data)
def manage_admin(): if request.method == 'GET': return render_template('manage_admin.html') else: user = User(request.form.get('admin_id'), request.form.get('admin_id'), request.form.get('admin_id'), request.form.get('privilidge')) admin = AdminUser(admin_id=request.form.get('admin_id'), admin_name=request.form.get('admin_name'), privilidge=request.form.get('privilidge')) if User.query.filter_by(user_id=request.form.get('admin_id')).first() is None: db.session.add(user) db.session.commit() db.session.add(admin) db.session.commit() return jsonify("add admin success") return jsonify("id already existed")
def test_password_hash(self): u = AdminUser(username="******", password="******") self.assertTrue(u.check_password('testpass')) self.assertFalse(u.check_password('incorrectpass'))
# Parse arguments parser = argparse.ArgumentParser() parser.add_argument('admin_username') parser.add_argument('admin_password') args = parser.parse_args() response = input( 'This will nuke your current database! Are you sure you want to continue? [y/n]' ) if response != 'y': exit() # Nuke db print('Nuking database...') db.drop_all() # Create db print('Creating schema...') db.create_all() # Create admin user print('Creating admin user...') admin_user = AdminUser() admin_user.username = args.admin_username admin_user.password = bcrypt.generate_password_hash(args.admin_password) db.session.add(admin_user) db.session.commit() print('Done!')