def get_user_info(_id): user = User() record = user.find_by_id(_id) if not record: return jsonify({ 'message': 'record not found', 'code': 104040 }), 404 relation = TeamUser().collection.find_one({'user_id': _id}) team = Team().find_by_id(relation.get('team_id')) record['team'] = team record.pop('password') permissions, roles = user.get_permissions(_id) hosts = user.get_hosts(_id) return jsonify({ 'message': 'ok', 'code': 0, 'data': { 'user': record, 'roles': roles, 'permissions': permissions, 'hosts': hosts, } })
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 get_profile(_id): user = User() record = user.find_by_id(_id) if not record: return jsonify({'message': 'record not found', 'code': 104040}), 404 relation = TeamUser().collection.find_one({'user_id': _id}) team = Team().find_by_id(relation.get('team_id')) record['team'] = team record.pop('password') record['team'] = team setting = db.collection('setting').find_one({}) options = { 'slack': True, 'sms': True, 'wechat': True, 'smtp': True, } if setting: slack = setting.get('slack') or {} sms = setting.get('nexmo') or {} wechat = setting.get('wechat') or {} smtp = setting.get('smtp') or {} options['slack'] = bool(slack.get('enable')) options['sms'] = bool(sms.get('enable')) options['wechat'] = bool(wechat.get('enable')) options['smtp'] = bool(smtp.get('enable')) record['setting'] = options return jsonify({ 'message': 'ok', 'code': 0, 'data': record, })
def bind_role(user_id): payload = request.get_json() if not payload: return jsonify({'message': 'invalid params', 'code': 104030}), 400 role_ids = payload.get('role_ids') if not role_ids or type(role_ids) != list: return jsonify({'message': 'invalid params', 'code': 104031}), 400 user = User() user_info = user.find_by_id(user_id) if not user_info: return jsonify({'message': 'record not found', 'code': 104040}), 404 roles = Role().find_by_ids(role_ids) if not roles: return jsonify({'message': 'invalid param', 'code': 104031}), 400 for role in roles: data = { 'user_id': user_id, 'role_id': str(role['_id']), } where = data.copy() data['created_at'] = time.time() data['add_by'] = login_user.get('username') db.collection('user_roles').update_one(where, {'$set': data}, upsert=True) return jsonify({ 'message': 'ok', 'code': 0, })
def save_profile(_id): payload = request.get_json() user = User() record = user.find_by_id(_id) if not record: return jsonify({ 'message': 'record not found', 'code': 104040 }), 404 nickname = payload.get('nickname') phone = payload.get('phone') email = payload.get('email') address = payload.get('address') wechat = payload.get('wechat') update = {} if nickname: update['nickname'] = nickname if phone: update['phone'] = phone if email: update['email'] = email if address: update['address'] = address if wechat: update['wechat'] = wechat if update: user.collection.update_one({'_id': record['_id']}, update={'$set': update}) 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, })