def add_user(self,name,password): #添加用户 try: print('当前权限'+self.user_obj.role) if self.user_obj.role == 'administrator': user_file_path = conf.users_db_filepath + '\\' + name if not os.path.exists(user_file_path): os.mkdir(user_file_path) user_file = user_file_path + '\\' + name self.user_db = shelve.open(user_file,flag = "c", writeback = True) password_encrypt = hash.hash(password) user_obj = users.Users(name,password_encrypt) #添加hash模块加密密码 self.user_db[name]=user_obj user_home_path = conf.FTP_BASE + '\\' + name #print(user_home_path) if not os.path.exists(user_home_path): os.mkdir(user_home_path) if os.path.exists(user_file + '.dat') and os.path.exists(user_home_path): print('添加用户%s成功' % name) self.user_db.close() else: print('该用户已存在,无法添加') except Exception as e: print(e)
def init_admin_user(self,filepath): #初始化添加默认用户 try: self.admin_db = shelve.open(filepath,flag = "c", writeback = True) admin_obj = users.Users('admin',hash.hash('123456')) self.admin_db['admin']=admin_obj except Exception as e: print(e)
def init_admin_user(self, filepath): ''' 添加默认用户admin , 密码123456 :param filepath: :return: ''' try: self.admin_db = shelve.open(filepath, flag="c", writeback=True) admin_obj = users.Users('admin', hash.hash('123456')) self.admin_db['admin'] = admin_obj except Exception as e: print(e)
def login(self): ''' 登陆验证,成功后进入管理菜单 :return: ''' try: errcount = 1 while errcount <= 3: name = input('请输入用户名:').strip() if name == 'admin': self.user_file_path = conf.admin_db_filepath + '\\' + 'admin' elif name == '': print('用户名非法') continue else: self.user_file_path = conf.users_db_filepath + '\\' + name if os.path.exists(self.user_file_path): self.db_dict = shelve.open(self.user_file_path + '\\' + name) #读取数据库获取用户信息 self.user_obj = self.db_dict[name] user_name = self.user_obj.name user_password = str(self.user_obj.password) password = str(input('请输入密码:')).strip() if user_name == name and user_password == hash.hash( password): print("登录成功,进入管理界面") self.name = user_name self.password = user_password self.user_home_path = conf.FTP_BASE + '\\' + user_name if not os.path.exists(self.user_home_path): print('用户FTP目录不存在,自动创建') os.makedirs(self.user_home_path) self.cur_path = self.user_home_path # 初始化用户目录,路径 self.manage_view(user_name) else: print('用户名密码错误,次数' + str(errcount)) errcount += 1 self.db_dict.close() else: print(self.user_file_path) print('用户文件不存在') break except Exception as e: print(e)
def post(self): valid = True self.values['username'] = self.request.get('username') self.values['email'] = self.request.get('email') self.values['password'] = self.request.get('password') self.values['verify'] = self.request.get('verify') if (not userregex.valid_email(self.values['email']) and (len(self.values['email']) > 0)): valid = False self.values['email_error'] = "Your e-mail address is not valid" if not userregex.valid_username(self.values['username']): valid = False self.values['username_error'] = "Your username is not valid" if not userregex.valid_password(self.values['password']): valid = False self.values['password_error'] = "Your password is not valid" if not (self.values['password'] == self.values['verify']): valid = False self.values['verify_error'] = "Your passwords do not match" user_test = User.by_username(self.values['username']) if user_test: valid = False self.values['username_error'] = 'Your username already exists' if valid: salt = hash.hash(str(time.time) + "secret value") hashed_password = hash.hash_with_salt(self.values['password'], salt) user = User(username=self.values['username'], hashed_password=hashed_password, salt=salt, email=self.values['email']) user.put() self.login(user) self.redirect("/") else: self.render_form(self.values)