def eliminar_telefono(uid=None, tid=None, token=None): prof = warden.has_one_profile(token, ['users-super-admin', 'users-admin']) if not prof['profile']: return ('no tiene los permisos suficientes', 403) assert uid != None assert tid != None with obtener_session() as session: UsersModel.eliminar_telefono(session, tid) session.commit() return {'id': tid}
def correos_de_usuario(uid, cid, token=None): admin = False prof = warden.has_all_profiles(token, ['users-super-admin']) if prof and prof['profile']: admin = True else: prof = warden.has_one_profile(token, ['users-admin', 'users-operator']) if prof: admin = prof['profile'] if not admin: auid = token['sub'] if auid != uid: return ('no tiene los permisos suficientes', 403) offset = request.args.get('offset', None, int) limit = request.args.get('limit', None, int) h = request.args.get('h', False, bool) with obtener_session() as session: return UsersModel.correos(session=session, usuario=uid, historico=h, offset=offset, limit=limit)
def usuarios_por_lista(uids=[], token=None): admin = False prof = warden.has_all_profiles(token, ['users-super-admin']) if prof and prof['profile']: admin = True else: prof = warden.has_one_profile(token, ['users-admin', 'users-operator']) if prof: admin = prof['profile'] if not admin: auid = token['sub'] if len(uids) != 1 and auid != uids[0]: return ('no tiene los permisos suficientes', 403) with obtener_session() as session: usuarios = [] for uid in uids: try: us = UsersModel.usuario(session=session, uid=uid) usuarios.append(us) except NoResultFound as e: logging.warn('{} no existe'.format(uid)) return usuarios
def usuarios_por_search(token=None): """ para poder debuggear el require valid token. token = warden._require_valid_token() if not token: return warden._invalid_token() """ search = request.args.get('q', None) offset = request.args.get('offset', None, int) limit = request.args.get('limit', None, int) admin = False prof = warden.has_all_profiles(token, ['users-super-admin']) if prof and prof['profile']: admin = True else: prof = warden.has_one_profile(token, ['users-admin', 'users-operator']) if prof: admin = prof['profile'] if not admin: return ('no tiene los permisos suficientes', 403) with obtener_session() as session: us = UsersModel.usuarios(session=session, search=search, offset=offset, limit=limit) return us
def eliminar_correo(uid=None, cid=None, token=None): if not uid: uid = token['sub'] prof = warden.has_one_profile(token, ['users-super-admin', 'users-admin']) if not prof['profile']: if uid != token['sub']: return ('no tiene los permisos suficientes', 403) assert uid != None assert cid != None with obtener_session() as session: UsersModel.eliminar_correo(session, cid) session.commit() return {'id': cid}
def chequear_precondiciones_de_usuario(uid, token=None): assert uid is not None prof = warden.has_one_profile(token, ['users-super-admin', 'users-admin']) if not prof['profile']: return ('no tiene los permisos suficientes', 403) with obtener_session() as s: return UsersModel.precondiciones(s, uid)
def usuario_por_dni(dni, token=None): prof = warden.has_all_profiles(token, ['users-super-admin']) if not prof or not prof['profile']: return ('Insuficient access', 401) with obtener_session() as s: u = UsersModel.usuario_por_dni(session=s, dni=dni) return u
def enviar_confirmar_correo(uid, cid, token=None): admin = False prof = warden.has_all_profiles(token, ['users-super-admin']) if prof and prof['profile']: admin = True else: prof = warden.has_all_profiles(token, ['users-admin']) if prof: admin = prof['profile'] if not admin: auid = token['sub'] if auid != uid: return ('no tiene los permisos suficientes', 403) with obtener_session() as session: UsersModel.enviar_confirmar_correo(session, cid) session.commit()
def crear_usuario(token=None): prof = warden.has_one_profile(token, ['users-super-admin', 'users-admin']) if not prof['profile']: return ('no tiene los permisos suficientes', 403) usuario = request.get_json() logging.debug(usuario) with obtener_session() as session: uid = UsersModel.crear_usuario(session, usuario) session.commit() return uid
def actualizar_usuario(uid, token=None): admin = False prof = warden.has_all_profiles(token, ['users-super-admin']) if prof and prof['profile']: admin = True else: prof = warden.has_all_profiles(token, ['users-admin']) if prof: admin = prof['profile'] if not admin: auid = token['sub'] if auid != uid: return ('no tiene los permisos suficientes', 403) datos = json.loads(request.data) with obtener_session() as session: UsersModel.actualizar_usuario(session, uid, datos) session.commit() return uid
def confirmar_correo(uid, cid, token=None): admin = False prof = warden.has_all_profiles(token, ['users-super-admin']) if prof and prof['profile']: admin = True else: prof = warden.has_all_profiles(token, ['users-admin']) if prof: admin = prof['profile'] if not admin: auid = token['sub'] if auid != uid: return ('no tiene los permisos suficientes', 403) assert cid is not None code = json.loads(request.data)['codigo'] with obtener_session() as session: UsersModel.confirmar_correo(session=session, cid=cid, code=code) session.commit()
def chequear_disponibilidad_cuenta(cuenta, token=None): prof = warden.has_one_profile(token, ['users-super-admin', 'users-admin']) if not prof['profile']: return ('no tiene los permisos suficientes', 403) with obtener_session() as session: correo = UsersModel.obtener_correo_por_cuenta(session=session, cuenta=cuenta) if correo: return {'existe': True, 'correo': correo} else: return {'existe': False, 'correo': None}
def agregar_correo_institucional(uid, token=None): prof = warden.has_one_profile(token, ['users-super-admin', 'users-admin']) if not prof['profile']: return ('no tiene los permisos suficientes', 403) assert uid != None datos = json.loads(request.data) assert datos['email'] != None with obtener_session() as session: if not UsersModel.existe(session=session, usuario=uid): raise Exception('Usuario no existente') mail = UsersModel.obtener_correo_por_cuenta(session=session, cuenta=datos['email']) if not mail: mail = UsersModel.agregar_correo_institucional(session=session, uid=uid, datos=datos) session.commit() else: mail.confirmado = datetime.datetime.now() session.commit() return mail.id
def agregar_correo(uid, token=None): admin = False prof = warden.has_all_profiles(token, ['users-super-admin']) if prof and prof['profile']: admin = True else: prof = warden.has_all_profiles(token, ['users-admin']) if prof: admin = prof['profile'] if not admin: auid = token['sub'] if auid != uid: return ('no tiene los permisos suficientes', 403) assert uid != None datos = json.loads(request.data) with obtener_session() as session: cid = UsersModel.agregar_correo(session=session, uid=uid, datos=datos) session.commit() UsersModel.enviar_confirmar_correo(session, cid) session.commit() return {'cid': cid}
def uuids_usuarios(token=None): admin = False prof = warden.has_all_profiles(token, ['users-super-admin']) if prof and prof['profile']: admin = True else: prof = warden.has_one_profile(token, ['users-admin', 'users-operator']) if prof: admin = prof['profile'] if not admin: return ('no tiene los permisos suficientes', 403) with obtener_session() as session: us = UsersModel.usuarios_uuids(session=session) return us
if len(n) >= 2: apellido = n[0].strip().capitalize() nombre = n[1].strip().capitalize() else: nombre = n[0].strip().capitalize() r = { 'cargo': cargo, 'dni': dni, 'nombre': nombre, 'apellido': apellido, 'nacimiento': nacimiento } #m = reg.match(cargo) #if m: u = UsersModel.usuario_por_dni(s, dni=dni) if not u: logging.debug( '{} {} {} no existe, se crea la persona'.format( dni, nombre, apellido)) UsersModel.crear_usuario(s, r) s.commit() else: logging.debug('{} {} {} ya existe, se actualiza'.format( dni, nombre, apellido)) uid = u.id logging.debug(r) UsersModel.actualizar_usuario(s, uid, r) s.commit()
if __name__ == '__main__': import sys u = {'dni': sys.argv[1], 'nombre': sys.argv[2], 'apellido': sys.argv[3]} print('Creando usuario {}'.format(u)) from users.model import obtener_session, UsersModel with obtener_session() as s: uid = UsersModel.crear_usuario(s, u) s.commit() UsersModel.cambiar_clave(s, uid, sys.argv[4]) s.commit()
def agregar_avatar(hash, token=None): f = request.files['file'] contenido = base64.b64encode(f.read()).decode('utf-8') UsersModel.actualizar_avatar(hash, contenido) return {'status': 'OK', 'status_code': 200}, 200
def chequear_precondiciones_usuario(token=None): uid = token['sub'] assert uid is not None with obtener_session() as s: return UsersModel.precondiciones(s, uid)
def obtener_avatar(hash): return UsersModel.obtener_avatar(hash=hash)