Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix admin automatic auth #42

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions dashboard/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@


def in_admin_session() -> bool:
if st.secrets.get("skip_auth", False):
return True
return is_user_logged() and st.session_state["user"] == os.environ["ADMIN"]
return st.session_state.get("is_admin", False)


def is_user_logged() -> bool:
Expand All @@ -30,7 +28,15 @@ def login(user):

def _load_user_model():
email = st.session_state.user
st.session_state.user_model = Person.find_one(emails=email)
user = Person.find(email=email)
is_admin = email == os.environ["ADMIN"]
st.session_state.user_model = Person(name='ADMIN', emails=[email])
if user:
st.session_state.user_model = user[0]
if is_admin:
st.session_state.is_admin = True

assert user or is_admin, "If the user is not admin it must be registered"


def current_user_model() -> Person:
Expand All @@ -48,6 +54,8 @@ def current_user_model() -> Person:

def logout():
del st.session_state["user"]
del st.session_state["user_model"]
del st.session_state["is_admin"]
delete_token_in_cookies()


Expand All @@ -58,6 +66,7 @@ def try_login_using_cookies():
return login(*credentials)
return None


def authenticate():
if st.secrets.get("skip_auth", False):
login(os.environ["ADMIN"])
Expand Down
111 changes: 59 additions & 52 deletions dashboard/import_csv.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,68 @@
import pandas as pd
from models import JournalPaper, Journal, Person, ConferencePresentation, Book, BookChapter
import Levenshtein
import pandas as pd
from models import (
Book,
BookChapter,
ConferencePresentation,
Journal,
JournalPaper,
Person,
)

df = pd.read_csv("/src/data/profesores.csv")

people = Person.all()

matches = {
'Jose Luis Castañeda Lorenzo' : 'José Luis Castañeda Lorenzo' ,
'Ernesto Alejandro Lopez Cadalso' : 'Ernesto Alejandro López Cadalso' ,
'Juan Pablo Consuegra Ayala' : 'Juan Pablo Consuegra Ayala' ,
'Suilan Estevez Velarde' : 'Suilan Estévez Velarde' ,
'Maria Elvira Fernandez Sa' : 'María Elvira Fernández Sa' ,
'Alejandro Piad Morffis' : 'Alejandro Piad Morffis' ,
'Carmen Teresa Fernandez Montoto' : 'Carmen Fernández' ,
'Lucina Garcia Hernandez' : 'Lucina García' ,
'Yudivian Almeida Cruz' : 'Yudivián Almeida Cruz' ,
'Luciano Garcia Garrido' : 'Luciano García' ,
'Alberto Fernandez Oliva' : 'Alberto Fernández ' ,
'Joanna Campbell Amos' : 'Joanna Campbell Amos' ,
'Juan Enrique Morales Calvo' : 'Juan Enrique Morales Calvo' ,
'Aracelys Garcia Armenteros' : 'Aracelys García Armenteros' ,
'Eduardo Quesada Orozco' : 'Eduardo Quesada Orozco' ,
'Gemayqzel Bouza Allende' : 'Gemayqzel Bouza Allende' ,
'Jose Alejandro Mesejo Chiong' : 'Jose A. Mesejo Chiong' ,
'Damian Valdes Santiago' : 'Damian Valdés Santiago' ,
'Fernando R. Rodriguez Flores' : 'Fernando Rodriguez Flores' ,
'Elianys Garcia - Pola Cordoves' : 'Elianys García-Pola Cordobes' ,
'Aymee De Los Angeles Marrero Severo' : 'Aymée Marrero Severo' ,
'Julian Sarria Gonzalez' : 'Julián Sarría González' ,
'Sofia Behar Jequin' : 'Sofía Behar Jequín' ,
'Yanet Garcia Serrano' : 'Yanet Garcia Serrano' ,
'Vivian Del R. Sistachs Vega' : 'Vivian del Rosario Sistachs Vega' ,
'Carlos Narciso Bouza Herrera' : 'Carlos Bouza Herrera' ,
'Sira Maria Allende Alonso' : 'Sira Allende Alonso' ,
'Miraida Ferras Ferras' : 'Miraida Ferras Ferras' ,
'Marta Lourdes Baguer Diaz Romanach' : 'Marta L. Baguer Diaz-Romanach' ,
'Angela Mireya Leon Mecias' : 'Ángela Mireya León Mecías' ,
'Frank Michel Enrique Hevia' : 'Frank Michel Enrique Hevia' ,
'Wilfredo Morales Lezca' : 'Wilfredo Morales Lezca' ,
'Marcel Ernesto Sanchez Aguilar' : 'Marcel Ernesto Sánchez Aguilar' ,
'Jorge Estrada Hernandez' : 'Jorge Estrada Hernández' ,
'Ernesto Luis Estevanell Valladares' : 'Ernesto Luis Estevanell Valladares' ,
'Daniel Alejandro Valdes Perez' : 'Daniel Valdés Pérez' ,
'Alejandro Roque Piedra' : 'Alejandro Roque Piedra' ,
'Jose Fidel Hernandez Advincula' : 'José Fidel Hernández Advíncula' ,
'Celia Tamara Gonzalez Gonzalez' : 'Celia T. González González' ,
'Juan Carlos Lopez Realpozo' : 'Juan Pablo Consuegra Ayala' ,
'Marleny Soler Martinez' : 'Marleny Soler Martínez' ,
'Roxana Cabrera Puig' : 'Roxana Cabrera Puig' ,
'Reynaldo Rodriguez Ramos' : 'Reinaldo Rodríguez Ramos' ,
'Mariano Rodriguez Ricard' : 'Mariano Rodríguez Ricard' ,
'Jose Enrique Valdes Castro' : 'Jose Enrique Valdes Castro' ,
'Joaquin Alberto Herrera Macias' : 'Joaquín Alberto Herrera Macías' ,
'Lisset Suarez Plasencia' : 'Lisset Suárez Plasencia' ,
'Yeneit Delgado Kios' : 'Yeneit Delgado Kios' ,
'Ernesto Alejandro Borrego Rodriguez' : 'Ernesto Borrego Rodríguez' ,
"Jose Luis Castañeda Lorenzo": "José Luis Castañeda Lorenzo",
"Ernesto Alejandro Lopez Cadalso": "Ernesto Alejandro López Cadalso",
"Juan Pablo Consuegra Ayala": "Juan Pablo Consuegra Ayala",
"Suilan Estevez Velarde": "Suilan Estévez Velarde",
"Maria Elvira Fernandez Sa": "María Elvira Fernández Sa",
"Alejandro Piad Morffis": "Alejandro Piad Morffis",
"Carmen Teresa Fernandez Montoto": "Carmen Fernández",
"Lucina Garcia Hernandez": "Lucina García",
"Yudivian Almeida Cruz": "Yudivián Almeida Cruz",
"Luciano Garcia Garrido": "Luciano García",
"Alberto Fernandez Oliva": "Alberto Fernández ",
"Joanna Campbell Amos": "Joanna Campbell Amos",
"Juan Enrique Morales Calvo": "Juan Enrique Morales Calvo",
"Aracelys Garcia Armenteros": "Aracelys García Armenteros",
"Eduardo Quesada Orozco": "Eduardo Quesada Orozco",
"Gemayqzel Bouza Allende": "Gemayqzel Bouza Allende",
"Jose Alejandro Mesejo Chiong": "Jose A. Mesejo Chiong",
"Damian Valdes Santiago": "Damian Valdés Santiago",
"Fernando R. Rodriguez Flores": "Fernando Rodriguez Flores",
"Elianys Garcia - Pola Cordoves": "Elianys García-Pola Cordobes",
"Aymee De Los Angeles Marrero Severo": "Aymée Marrero Severo",
"Julian Sarria Gonzalez": "Julián Sarría González",
"Sofia Behar Jequin": "Sofía Behar Jequín",
"Yanet Garcia Serrano": "Yanet Garcia Serrano",
"Vivian Del R. Sistachs Vega": "Vivian del Rosario Sistachs Vega",
"Carlos Narciso Bouza Herrera": "Carlos Bouza Herrera",
"Sira Maria Allende Alonso": "Sira Allende Alonso",
"Miraida Ferras Ferras": "Miraida Ferras Ferras",
"Marta Lourdes Baguer Diaz Romanach": "Marta L. Baguer Diaz-Romanach",
"Angela Mireya Leon Mecias": "Ángela Mireya León Mecías",
"Frank Michel Enrique Hevia": "Frank Michel Enrique Hevia",
"Wilfredo Morales Lezca": "Wilfredo Morales Lezca",
"Marcel Ernesto Sanchez Aguilar": "Marcel Ernesto Sánchez Aguilar",
"Jorge Estrada Hernandez": "Jorge Estrada Hernández",
"Ernesto Luis Estevanell Valladares": "Ernesto Luis Estevanell Valladares",
"Daniel Alejandro Valdes Perez": "Daniel Valdés Pérez",
"Alejandro Roque Piedra": "Alejandro Roque Piedra",
"Jose Fidel Hernandez Advincula": "José Fidel Hernández Advíncula",
"Celia Tamara Gonzalez Gonzalez": "Celia T. González González",
"Juan Carlos Lopez Realpozo": "Juan Pablo Consuegra Ayala",
"Marleny Soler Martinez": "Marleny Soler Martínez",
"Roxana Cabrera Puig": "Roxana Cabrera Puig",
"Reynaldo Rodriguez Ramos": "Reinaldo Rodríguez Ramos",
"Mariano Rodriguez Ricard": "Mariano Rodríguez Ricard",
"Jose Enrique Valdes Castro": "Jose Enrique Valdes Castro",
"Joaquin Alberto Herrera Macias": "Joaquín Alberto Herrera Macías",
"Lisset Suarez Plasencia": "Lisset Suárez Plasencia",
"Yeneit Delgado Kios": "Yeneit Delgado Kios",
"Ernesto Alejandro Borrego Rodriguez": "Ernesto Borrego Rodríguez",
}

for i, row in df.iterrows():
Expand All @@ -68,7 +75,7 @@
else:
person = Person(name=name)

person.institution="Universidad de La Habana"
person.institution = "Universidad de La Habana"
person.faculty = "Matemática y Computación"
person.department = department
person.scientific_grade = academic_grade
Expand Down
6 changes: 2 additions & 4 deletions dashboard/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from models.custom_model import CustomModel
from models.data_models.award_model import Award
from models.data_models.book_chapter_model import BookChapter
from models.data_models.book_model import Book
from models.data_models.classes_model import Classes
from models.data_models.conference_presentation_model import (
ConferencePresentation,
)
from models.data_models.conference_presentation_model import ConferencePresentation
from models.data_models.journal_model import Journal
from models.data_models.journal_paper_model import JournalPaper
from models.data_models.person_model import Person
Expand All @@ -13,4 +12,3 @@
from models.data_models.research_group_model import ResearchGroup
from models.data_models.subject_model import Subject
from models.data_models.thesis_model import Thesis
from models.custom_model import CustomModel
2 changes: 0 additions & 2 deletions dashboard/models/custom_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def with_refs(model_class: ModelT) -> ModelT:
fields = model_class.__fields__
for field_name, field in fields.items():
if isclass(field.type_) and issubclass(field.type_, Ref):

# Function that returns a validator for a Ref[T] field
def _ref_val_wrapper(field):
def ref_val(cls, value):
Expand All @@ -62,7 +61,6 @@ def ref_val(cls, value):
)

elif isclass(field.type_) and issubclass(field.type_, RefList):

# Function that returns a validator for a RefList[T] field
def _reflist_val_wrapper(field):
def ref_list_val(cls, value):
Expand Down
3 changes: 1 addition & 2 deletions dashboard/models/data_models/book_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pydantic import HttpUrl

from models.custom_model import collection_name, with_refs
from models.data_models.publication_model import Publication
from pydantic import HttpUrl


@with_refs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pydantic import HttpUrl

from models.custom_model import collection_name, with_refs
from models.data_models.publication_model import Publication
from pydantic import HttpUrl


@with_refs
Expand Down
3 changes: 1 addition & 2 deletions dashboard/models/data_models/journal_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import List

from pydantic import Field, HttpUrl

from models.custom_model import CustomModel, collection_name
from pydantic import Field, HttpUrl


@collection_name("journals")
Expand Down
3 changes: 1 addition & 2 deletions dashboard/models/data_models/journal_paper_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pydantic import HttpUrl

from models.custom_model import Ref, collection_name, with_refs
from models.data_models.journal_model import Journal
from models.data_models.person_model import Person
from models.data_models.publication_model import Publication
from pydantic import HttpUrl


@with_refs
Expand Down
1 change: 0 additions & 1 deletion dashboard/models/data_models/project_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import List

import streamlit as st

from models.custom_model import CustomModel, Ref, RefList, collection_name, with_refs
from models.data_models.person_model import Person

Expand Down
6 changes: 1 addition & 5 deletions dashboard/models/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
WRITE = 2
ADMIN = 4

PERMISSIONS = {
READ: "Lectura",
WRITE: "Escritura",
ADMIN: "Administrador"
}
PERMISSIONS = {READ: "Lectura", WRITE: "Escritura", ADMIN: "Administrador"}

PERMISSIONS_BY_NAME = {v: k for k, v in PERMISSIONS.items()}
ALL_PERMISSIONS = reduce(lambda p1, p2: p1 | p2, PERMISSIONS.keys())
Expand Down
Loading