def delete_user(_id): is_admin = login_user.get('is_admin') if not is_admin: return jsonify({ 'message': 'admin required', 'code': 104033, }), 403 record = User.find_by_id(_id) if not record: return jsonify({ 'message': 'record not found', 'code': 104040, }), 404 update = { '$set': { 'status': -1, 'delete_at': time.time(), } } condition = {'_id': record['_id']} User.update_one(condition, update=update) TeamMember.delete_one({'user_id': _id}) user_roles = UserRole.find(condition) for item in user_roles: where = {'_id': item['_id']} UserRole.delete_one(where) return jsonify({ 'message': 'ok', 'code': 0, })
def update_user(_id): payload = request.get_json() record = User.find_by_id(_id) if not record: return jsonify({ 'message': 'record not found', 'code': 104040 }), 404 if not payload: return jsonify({ 'message': 'illegal params', 'code': 104000 }), 400 current_user_id = login_user.get('user_id') is_admin = login_user.get('is_admin') username = payload.get('username') nickname = payload.get('nickname') email = payload.get('email') phone = payload.get('phone') role_ids = payload.get('role') team_id = payload.get('team_id') address = payload.get('address') # current_team_id = payload.get('currentTeamId') # current_role_ids = payload.get('currentRoleIds') if not is_admin: return jsonify({ 'message': 'bad permission', 'code': 104130 }), 403 update = {} if username and record['username'] != username: update['username'] = username check = User.find_one({'username': username}) if check: return jsonify({ 'message': 'username existed', 'code': 104001 }), 400 if email and record.get('email') != email: update['email'] = email check = User.find_one({'email': email}) if check: return jsonify({ 'message': 'email existed', 'code': 104001 }), 400 if phone and record.get('phone') != phone: update['phone'] = phone check = User.find_one({'phone': phone}) if check: return jsonify({ 'message': 'phone existed', 'code': 104001 }), 400 if nickname: update['nickname'] = nickname if address: update['address'] = address if team_id: change = { '$set': { 'team_id': team_id, 'user_id': _id, 'updated_at': time.time(), } } condition = { 'user_id': _id, } db.collection('team_members').update_one(condition, update=change, upsert=True) if role_ids: result = User().bind_roles(_id, role_ids, add_by=login_user.get('username')) User.update_one({'_id': record['_id']}, {'$set': update}) return jsonify({ 'message': 'ok', 'code': 0, })