def initial_create(): user_count = User.query.count() if user_count == 0: if request.method == 'GET': return render_template('users/admin/initial_create.html') else: user_info = { 'email': request.form.get('email'), 'fname': request.form.get('fname'), 'lname': request.form.get('lname'), 'password': request.form.get('password'), 'type': 'admin' } user = User(user_info) user.status = status.ADMIN DB.session.add(user) DB.session.commit() # add admin role to the user role = UserRole(user.id) role.name = 'admin' DB.session.add(role) DB.session.commit() login_user(user, remember=True) return redirect(url_for('admin-dash')) else: return 'Cannot create new admin'
def _delete_profile_and_related(self, profile): print('\tDeleting user and related data: ' + profile.o365Email) UserRole.delete().where( UserRole.o365UserId == profile.o365UserId).execute() TokenCache.delete().where( TokenCache.o365UserId == profile.o365UserId).execute() profile.delete_instance() profile.user.delete_instance()
def handlePostRequest(self): action = self.request.get('action', None) data = None try: if action != self.ACTION_ADMIN: data = self.request.POST['data'] except KeyError: self.addMessage('You must upload a file.') else: if action == self.ACTION_HAZARD: key = blobstore.parse_blob_info(data) zfile = ZipFile(blobstore.BlobReader(key), 'r') reader = HazardsImporter(zfile) if reader.read(): self.addMessage('The operation was successful.', self.MSG_TYPE_SUCCESS) else: self.addMessage('<strong>Error</strong>: %s' % reader.error) key.delete() if action == self.ACTION_INGREDIENT: key = blobstore.parse_blob_info(data) user = User.load(users.get_current_user()) if not user: user = User.system_user() reader = IngredientsImporter(blobstore.BlobReader(key), user) if reader.read(): self.addMessage('The operation was successful.', self.MSG_TYPE_SUCCESS) else: self.addMessage('<strong>Error</strong>: %s' % reader.error) key.delete() if action == self.ACTION_ADMIN: email = self.request.get('email') user = User.find_by_email(email) if not user: user = User.create_by_email(email) admin_role = Role.get_admin_role() UserRole.create(user, admin_role) self.addMessage( 'The admin "%s" was successfully added.' % email, self.MSG_TYPE_SUCCESS) self.addCommon() self.setActivePage('Utilities')
def setUp(self): self.app = web.app.test_client() self.ref1 = User("17788000001", None, None, 'WEB') self.ref2 = User("17788000002", None, None, 'WEB') self.ref1.set_password("123abc") self.ref2.set_password("123abc") db.session.add_all((self.ref1, self.ref2)) db.session.commit() self.users = [ self._moke_user(1, self.ref1.login_name, 'C1', 'ACTIVE'), self._moke_user(2, self.ref1.login_name, 'C1', 'INACTIVE'), self._moke_user(3, self.ref1.login_name, 'C1', 'ACTIVE'), self._moke_user(4, self.ref1.login_name, 'C1', 'ACTIVE'), self._moke_user(5, self.ref1.login_name, 'C1', 'ACTIVE'), self._moke_user(6, self.ref1.login_name, 'C1', 'ACTIVE'), self._moke_user(7, self.ref1.login_name, 'C1', 'INACTIVE'), self._moke_user(8, self.ref1.login_name, 'C2', 'ACTIVE'), self._moke_user(9, self.ref1.login_name, 'C1', 'ACTIVE'), self._moke_user(10, self.ref1.login_name, 'C1', 'ACTIVE'), self._moke_user(11, self.ref2.login_name, 'C2', 'INACTIVE'), self._moke_user(12, self.ref2.login_name, 'C3', 'ACTIVE'), self._moke_user(13, self.ref2.login_name, 'C1', 'ACTIVE'), self._moke_user(14, self.ref2.login_name, 'C4', 'ACTIVE'), self._moke_user(15, self.ref2.login_name, 'C4', 'ACTIVE'), self._moke_user(16, self.ref2.login_name, 'C4', 'ACTIVE'), ] self.user_roles = [UserRole(u.login_name, "USER") for u in self.users] db.session.add_all(self.users) db.session.add_all(self.user_roles) db.session.commit()
def user_has_role(user_info, role_name): has_role = False if user_info is not None: role = UserRole.get_role_for_user(user_info) role_flag = ROLE_FLAG_DICT.get(role_name) has_role = role & role_flag return has_role
def add_user_role(self, entity): if entity.name == None: raise tt.InvalidValueException(2, "no role name value exception") entity = UserRole(entity) self.session.add(entity) self.session.commit() print("Processing user role {0}".format(entity.name)) return True
def create(self, form): u = User(form.mobile.data, form.referrer.data, form.channel.data, form.source.data) u.set_password(form.password.data) db.session.add(u) ur = UserRole(u.login_name, 'USER') db.session.add(ur) db.session.commit() return u.as_dict()
def give_role_to_user(user_id, role_id): """ Gives a role to the user in database and Discord. Returns the given role or None if failed. """ role = UserRole(user_id=user_id, role_id=role_id) session.add(role) session.commit() return role
def UserMain(request): rolename = utils.get_rolename_by_username(request.user) return_dict = {'rolename':rolename, 'username':request.user, 'windowname':'None'} #add user adduserform = AddUserForm() return_dict['addUserForm'] = adduserform edituserform = EditUserForm() return_dict['editUserForm'] = edituserform if request.method == "POST": if request.POST.get('formtype') == 'add': adduserform = AddUserForm(request.POST) if adduserform.is_valid(): if User.objects.filter(username=adduserform.fields.get('username')).count() == 0: user = User.objects.create_user(adduserform.fields.get('username'), adduserform.fields.get('email'),\ adduserform.fields.get('password')) user.save() userrole_list = UserRole.objects.filter(username=adduserform.fields.get('username')) if userrole_list.count() != 0: userrole_list.delete() userrole = UserRole(username=user.username, rolename=adduserform.fields.get('rolename'),\ domain=adduserform.fields.get('domain'),realname=adduserform.fields.get('realname')) userrole.save() return_dict['windowname'] = 'successwindow' return_dict['windowmessage'] = u'增加用户成功' else: return_dict['windowname'] = 'errorwindow' return_dict['windowmessage'] = u'用户名已存在,请更换用户名' else: return_dict['windowname'] = 'errorwindow' return_dict['windowmessage'] = adduserform.error_message if request.POST.get('formtype') == 'edit': edituserform = EditUserForm(request.POST) if edituserform.is_valid(): user = User.objects.get(username=edituserform.fields.get('username')) userrole = UserRole.objects.get(username=edituserform.fields.get('username')) user.email = edituserform.fields.get('email') userrole.rolename, userrole.domain, userrole.realname = \ edituserform.fields.get('rolename'),edituserform.fields.get('domain'),edituserform.fields.get('realname') user.save() userrole.save() return_dict['windowname'] = 'successwindow' return_dict['windowmessage'] = u'修改用户属性成功' else: return_dict['windowname'] = 'errorwindow' return_dict['windowmessage'] = edituserform.error_message if request.POST.get('formtype') == 'delete': deleteusername = request.POST.get('username','') print 'deleteusername:'******'windowname'] = 'errorwindow' return_dict['windowmessage'] = u'不能删除自己' User.objects.filter(username=deleteusername).delete() UserRole.objects.filter(username=deleteusername).delete() return_dict['windowname'] = 'successwindow' return_dict['windowmessage'] = u'删除用户成功' #list user users = User.objects.all() userinfos = [] for user in users: userinfos.append(UserInfo(user)) paginator = Paginator(userinfos, 25) try: page = request.GET.get('page','1') userinfos = paginator.page(page) except PageNotAnInteger: userinfos = paginator.page(1) except EmptyPage: userinfos = paginator.page(paginator.num_pages) except Exception: userinfos = paginator.page(1) return_dict['userlist'] = userinfos print userinfos.number return render_to_response('UserManage/user.html',return_dict, RequestContext(request))