def edit_user(user_id): response = service.get_user(user_id=user_id) if response.ok: user = response.json() else: flash(f"Unable to retrieve user by user id: {user_id}", "danger") return redirect(url_for("users.get_all_users")) form = EditUserForm() if request.method == "POST": if form.validate_on_submit(): response = service.edit_user(user_id=form.id.data, first_name=form.first_name.data, last_name=form.last_name.data, email=form.email.data) if response.ok: flash(f"User with id: {user_id} was edited successfully", "success") return redirect(url_for("users.get_all_users")) else: flash(f"There was an error saving the user because {response.text}", "danger") else: flash("There was an error editing the user", "danger") else: form.first_name.data = user["first_name"] form.last_name.data = user["last_name"] form.id.data = user["id"] form.email.data = user["email"] return render_template("edit_user.html", title="Edit User", form=form)
def login(): form = forms.LoginForm() if request.method == 'POST': # flash(f'login for user {form.username.data}, remember me {form.remember_me.data}') name= form.username.data passw = form.password.data email = form.email.data if not email or not passw: flash(NO_TITLE) else: user_name = service.check_pass(email, passw) if user_name is None: user_name = '' flash('bad credentionals') return render_template(url_for('login') + HT,form=form) # for u in user_name: user_name = service.get_user(email) flash(f'hi {user_name["name"]} #{user_name["id"]}!') user_id = user_name['id'] return redirect(url_for('index')) return render_template('login.html', form=form)
def start_accounting(self,req,nas): service.incr_stat(service.STAT_ACCT_START) _key = '%s_%s'%(nas.ip_addr,req.get_acctsessionid()) if service.is_online(_key): radiuslog.error('[Acct] accounting of start is repeated') return user = service.get_user(req.get_username()) if not user: radiuslog.error("[Acct] Received an accounting request but user[%s] not exists"%req.get_username()) return online = dict(user = user, nas = nas, sessionid = req.get_acctsessionid(), acctstarttime = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S"), framedipaddr = req.get_framedipaddr(), macaddr = req.get_macaddr(), nasportid = req.get_nasportid(), startsource = STATUS_TYPE_START) service.add_online(_key,online) if radiuslog.isEnabledFor(DEBUG): radiuslog.info('[Acct] User[%s],Nas[%s] billing starting'%(user.user_name,nas.ip_addr))
def update_accounting(self,req,nas): service.incr_stat(service.STAT_ACCT_UPDATE) _key = '%s_%s'%(nas.ip_addr,req.get_acctsessionid()) online = service.get_online(_key) if online is None: user = service.get_user(req.get_username()) if not user: radiuslog.error("[Acct] Received an accounting request but user[%s] not exists"%req.get_username()) return sessiontime = req.get_acctsessiontime() updatetime = datetime.datetime.now() _starttime = updatetime + datetime.timedelta(seconds=-sessiontime) online = dict(user = user, nas = nas, sessionid = req.get_acctsessionid(), acctstarttime = _starttime, framedipaddr = req.get_framedipaddr(), macaddr = req.get_macaddr(), nasportid = req.get_nasportid(), startsource = STATUS_TYPE_UPDATE) service.add_online(_key,online) else: online['framedipaddr'] = req.get_framedipaddr()
def get_users_favoriting_tweet(id_): try: check_tweet_existence(id_) favorites = Favorite.query.filter_by(tweet_id=id_).all() users = [get_user(favorite.user_id) for favorite in favorites] return jsonify(users=users) except Exception as e: return str(e)
def stop_accounting(self,req,nas): _key = '%s_%s'%(nas.ip_addr,req.get_acctsessionid()) service.incr_stat(service.STAT_ACCT_STOP) online = service.rmv_online(_key) ticket = req.get_ticket() if online is None: sessiontime = req.get_acctsessiontime() _stoptime = datetime.datetime.now() _starttime = _stoptime + datetime.timedelta(seconds=-sessiontime) ticket['acctstarttime'] = _starttime.strftime( "%Y-%m-%d %H:%M:%S") ticket['acctstoptime'] = _stoptime.strftime( "%Y-%m-%d %H:%M:%S") ticket['startsource'] = STATUS_TYPE_STOP ticket['stopsource'] = STATUS_TYPE_STOP ticket['nodeid'] = nas.id else: ticket['nodeid'] = online['user'].node_id ticket['acctstarttime'] = online['acctstarttime'] ticket['acctstoptime'] = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S") ticket['startsource'] = online['startsource'] ticket['stopsource'] = STATUS_TYPE_STOP ticket['framedipaddr'] = online['framedipaddr'] if radiuslog.isEnabledFor(DEBUG): radiuslog.info('[Acct] User[%s],Nas[%s] billing stop'%(req.get_username(),nas.ip_addr)) user = service.get_user(req.get_username()) if not user: ticketlog.info(json.dumps(ticket)) return product = service.get_product(user.product_id) if product.policy == 0: sessiontime = round(req.get_acctsessiontime()/60,0) usedfee = round(sessiontime/60*product.fee_price,0) remaind = round(sessiontime%60,0) if remaind > 0 : usedfee = usedfee + round(remaind*product.fee_price/60,0); balance = user.balance - usedfee balance = balance < 0 and 0 or balance service.update_user_balance(user.user_name,balance) ticket['actualfee'] = usedfee ticket['acctfee'] = usedfee ticket['isdeduct'] = 1 ticketlog.info("ticket:%s"%json.dumps(ticket)) else : ticket['actualfee'] = 0 ticket['acctfee'] = 0 ticket['isdeduct'] = 0 ticketlog.info("ticket:%s"%json.dumps(ticket))
def get_user(user_id): res = service.get_user(user_id) if res: return jsonify(user_id=str(res.id), email=res.email, role=res.role, firstname=res.firstname, lastname=res.lastname, phone=res.phone, address=res.address), 200 else: return jsonify({"success": False, "error": "Invalid user id"}), 200
def create_account(): # Send data to server if request.method == 'POST': username = request.form.get("username") # data = request.json # username = data["username"] # all quiz ids of the user user_data = get_user(username) if not user_data: user_data = {"username": username, "quizIDs": []} store_json(user_data, "userData") # Send data to server if request.method == 'GET': username = request.args.get("username") # all quiz ids of the user user_data = get_user(username) if not user_data: user_data = {"username": username, "quizIDs": []} store_json(user_data, "userData") return make_response(jsonify(user_data), 200)
def register(): if request.method == 'POST': form = forms.LoginForm() # flash(f'login for user {form.username.data}, remember me {form.remember_me.data}') app.logger.error(request) # name = request.form['fullname'] name= form.username.data passw = form.password.data email = form.email.data # name= passw= email ='' if not email or not passw or not name: flash(NO_TITLE) else: service.create_user(name, passw, email) user_name = service.get_user(email) flash(f'user {user_name["name"]} #{user_name["id"]} created') user_id = user_name['id'] return redirect(url_for('index')) return render_template('login.html', form=form)
def process(self, req): attr_keys = req.keys() if is_debug(): radiuslog.info("::Received an authentication request") radiuslog.info("Attributes: ") for attr in attr_keys: radiuslog.info("%s: %s" % (attr, req[attr])) nasaddr = req.get_nasaddr() macaddr = req.get_macaddr() nas = service.get_nas(nasaddr) # check roster if service.in_black_roster(macaddr): return self.send_reject(req, nas, 'user in black roster') vlanid, vlanid2 = req.get_vlanids() username1 = req.get_username() domain = None username2 = username1 if "@" in username1: username2 = username1[:username1.index("@")] req["User-Name"] = username2 domain = username1[username1.index("@") + 1:] if not service.user_exists(username2): return self.send_reject(req, nas, 'user not exists') user = service.get_user(username2) if user.status != 1: return self.send_reject(req, nas, 'Invalid user status') if domain and domain not in user.domain_code: return self.send_reject(req, nas, 'user domain does not match') if nasaddr not in service.get_nas_ips(user.node_id): return self.send_reject(req, nas, 'node does not match') if not utils.is_valid_date(user.auth_begin_date, user.auth_end_date): return self.send_reject(req, nas, 'user is not effective or expired') userpwd = utils.decrypt(user.password) if not req.is_valid_pwd(userpwd): return self.send_reject(req, nas, 'user password does not match') uproduct = service.get_product(user.product_id) if not uproduct: return self.send_reject(req, nas, 'user product does not match') if uproduct.policy == service.POLICY_TIMING and user.time_length <= 0: return self.send_reject(req, nas, 'user does not have the time length') if not self.verify_macaddr(user, macaddr): return self.send_reject(req, nas, 'user macaddr bind not match') valid_vlanid = self.verify_vlan(user, vlanid, vlanid2) if valid_vlanid == 1: return self.send_reject(req, nas, 'user vlanid does not match') elif valid_vlanid == 2: return self.send_reject(req, nas, 'user vlanid2 does not match') if user.concur_number > 0: if user.concur_number <= service.get_online_num(user.user_name): return self.send_reject(req, nas, 'user concur_number control') return self.send_accept( req, nas, **dict(ipaddr=user.ip_addr, bandcode=uproduct.bandwidth_code, input_max_limit=str(uproduct.input_max_limit), output_max_limit=str(uproduct.output_max_limit), input_rate_code=uproduct.input_rate_code, output_rate_code=uproduct.output_rate_code, domain_code=user.domain_code))
def process(self,req): attr_keys = req.keys() if is_debug(): radiuslog.info("::Received an authentication request") radiuslog.info("Attributes: ") for attr in attr_keys: radiuslog.info( "%s: %s" % (attr, req[attr])) nasaddr = req.get_nasaddr() macaddr = req.get_macaddr() nas = service.get_nas(nasaddr) # check roster if service.in_black_roster(macaddr): return self.send_reject(req,nas,'user in black roster') vlanid,vlanid2 = req.get_vlanids() username1 = req.get_username() domain = None username2 = username1 if "@" in username1: username2 = username1[:username1.index("@")] req["User-Name"] = username2 domain = username1[username1.index("@")+1:] if not service.user_exists(username2): return self.send_reject(req,nas,'user not exists') user = service.get_user(username2) if user.status != 1: return self.send_reject(req,nas,'Invalid user status') if domain and domain not in user.domain_code: return self.send_reject(req,nas,'user domain does not match') if nasaddr not in service.get_nas_ips(user.node_id): return self.send_reject(req,nas,'node does not match') if not utils.is_valid_date(user.auth_begin_date,user.auth_end_date): return self.send_reject(req,nas,'user is not effective or expired') userpwd = utils.decrypt(user.password) if not req.is_valid_pwd(userpwd): return self.send_reject(req,nas,'user password does not match') uproduct = service.get_product(user.product_id) if not uproduct: return self.send_reject(req,nas,'user product does not match') if uproduct.policy == service.POLICY_TIMING and user.time_length <= 0: return self.send_reject(req,nas,'user does not have the time length') if not self.verify_macaddr(user,macaddr): return self.send_reject(req,nas,'user macaddr bind not match') valid_vlanid = self.verify_vlan(user,vlanid,vlanid2) if valid_vlanid == 1: return self.send_reject(req,nas,'user vlanid does not match') elif valid_vlanid == 2: return self.send_reject(req,nas,'user vlanid2 does not match') if user.concur_number > 0: if user.concur_number <= service.get_online_num(user.user_name): return self.send_reject(req,nas,'user concur_number control') return self.send_accept(req,nas,**dict(ipaddr=user.ip_addr, bandcode=uproduct.bandwidth_code, input_max_limit=str(uproduct.input_max_limit), output_max_limit=str(uproduct.output_max_limit), input_rate_code=uproduct.input_rate_code, output_rate_code=uproduct.output_rate_code, domain_code=user.domain_code))