def users_page(): user = get_user_session() # Check login if not user: return redirect('/login') # Check is user admin if not user[3]: return redirect('/panel') if request.method == 'POST': try: if not request.form['password']: flash('Введите пароль', 'error') if not request.form['username']: flash('Введите юзернейм', 'error') if not get_flashed_messages(category_filter='error'): create_user(request.form['username'], request.form['password']) except sqlite3.IntegrityError: flash('Пользователь с таким юзернеймом уже существует', 'error') users = get_all_users() return render_template('users.html', page='users', user=user, users_list=users)
def create_user(self, email, password, role, network_id=None, gateway_id=None): with self.app.test_request_context(): user = users.create_user(email=email, password=encrypt_password(password)) user.network_id = network_id user.gateway_id = gateway_id role = Role.query.filter_by(name=role).first_or_404() user.roles.append(role) db.session.commit()
def valid_login(username, password): if not username or not password: return False users = get_all_users() # Create admin if there is no users if len(users) < 1: create_user(username, password, 1) # Check user exists with username/password user = get_user(username, password) # Auth user if user: auth_user(user[0]) return True flash('Ошибка входа', 'error') return False
def register(): ''' 用户注册接口 todo: 用户名支持中文,用户名和密码加正则匹配检验 ''' username = request.json.get('username') email = request.json.get('email') # 判断当前用户填写的注册邮箱和用户名是否已被注册 if user_by_username(username) is not None: return jsonify(type='param error', msg='username is been used.') if user_by_email(email) is not None: return jsonify(type='param error', msg='email is been used.') # 创建新用户并写入数据库 uid = create_user(username, email, request.json.get('passwd')) if uid == 0: return jsonify(type='db error', msg='register fail') return jsonify(type='OK', msg='register seccess')
def create_user(email, password, role, network=None, gateway=None, quiet=True): if email is None: email = prompt('Email') if password is None: password = prompt_pass('Password') confirmation = prompt_pass('Confirm Password') if password != confirmation: print "Passwords don't match" return if role == 'network-admin': if network is None: print 'Network is required for a network admin' return if gateway is not None: print 'Gateway is not required for a network admin' return if role == 'gateway-admin': if network is None: print 'Network is required for a gateway admin' return if gateway is None: print 'Gateway is required for a gateway admin' return user = users.create_user(email=email, password=encrypt_password(password)) user.network_id = network user.gateway_id = gateway if role is not None: role = Role.query.filter_by(name=role).first() user.roles.append(role) db.session.commit() if not quiet: print 'User created'