Verified Commit fc83fca6 authored by Peter Stanko's avatar Peter Stanko
Browse files

Gitlab username little improvements

parent 85656293
Pipeline #31386 passed with stage
in 7 minutes and 22 seconds
......@@ -104,9 +104,10 @@ USERS
@click.option('-p', '--password', help='Users password', prompt=True, hide_input=True,
confirmation_prompt=True)
@click.option('-s', '--secret', help="User's secret")
def cli_users_create(name, password, secret):
@click.option('-G', '--gitlab', help="Gitlab username")
def cli_users_create(name, password, secret, gitlab):
log.info(f"[CMD] Create User: {name}")
manager.create_admin_user(name, password, secret)
manager.create_admin_user(name, password, secret, gitlab)
@users_cli.command('set-password', help='Sets password for the user')
......
......@@ -17,6 +17,7 @@ ENVIRONMENT="${ENVIRONMENT:-dev}"
ADMIN_USER="${ADMIN_USER:-admin}"
ADMIN_PASSWORD="${ADMIN_PASSWORD:-789789}"
ADMIN_SECRET="${ADMIN_SECRET:-devel-admin-secret}"
ADMIN_GITLAB="${ADMIN_GITLAB}"
# INIT
echo "[INIT] Initializing the database"
......@@ -24,7 +25,7 @@ flask db upgrade
echo "[INIT] Initializing data for env: $ENVIRONMENT"
flask data init ${ENVIRONMENT}
echo "[INIT] Initializing admin user: ${ADMIN_USER}"
flask users create ${ADMIN_USER} --password ${ADMIN_PASSWORD} --secret ${ADMIN_SECRET}
flask users create ${ADMIN_USER} --password ${ADMIN_PASSWORD} --secret ${ADMIN_SECRET} --gitlab ${ADMIN_GITLAB}
if [ "$DATASETS" != "" ]; then
echo "[INIT] Additional datasets: $DATASETS"
......
......@@ -13,8 +13,8 @@ from management.data.data_dev import init_dev_data
from management.data.data_prod import init_prod_data
from management.data.data_test import init_test_data
from portal import logger
from portal.database.models import Course, Role, Secret, Submission, User
from portal.database import SubmissionState
from portal.database.models import Course, Role, Secret, Submission, User
from portal.service.find import FindService
from portal.service.users import UserService
from portal.tools import time
......@@ -94,7 +94,8 @@ class DataManagement:
log.debug(f"[DATA] User's password updated: {user.log_name}")
return user
def create_admin_user(self, name: str, password: str, secret: str = None) -> User:
def create_admin_user(self, name: str, password: str, secret: str = None, gitlab: str = None) \
-> User:
"""Creates new admin user with username
Args:
name(str): Username of the user
......@@ -107,6 +108,7 @@ class DataManagement:
username=name,
name=name,
admin=True,
gitlab_username=gitlab,
password=password)
if secret:
user.secrets.append(Secret(name='admin-secret', value=secret))
......
......@@ -101,11 +101,12 @@ class DataFactory:
return self._db.session
def create_user(self, username: str, uco=0, name=None, password=None,
email=None, admin=False) -> User:
email=None, admin=False, gitlab_username: str = None) -> User:
email = email or f"{username}@example.com"
password = password or "123456"
user = self.__create_entity(
User, uco=uco, username=username, is_admin=admin, email=email)
User, uco=uco, username=username, is_admin=admin, email=email,
gitlab_username=gitlab_username)
user.name = name or f"{username.capitalize()} User"
user.set_password(password=password)
return user
......
......@@ -118,10 +118,9 @@ class Secret(db.Model, EntityBase):
value = db.Column(db.String(120))
expires_at = db.Column(db.TIMESTAMP, nullable=True)
client_id = db.Column(db.String(36), db.ForeignKey(
'client.id', ondelete='cascade'), nullable=False)
client = db.relationship(
"Client", back_populates="secrets", uselist=False)
client_id = db.Column(db.String(36),
db.ForeignKey('client.id', ondelete='cascade'), nullable=False)
client = db.relationship("Client", back_populates="secrets", uselist=False)
def __init__(self, name: str, value: str = None, expires_at=None):
# TODO: check date/timestamp
......@@ -164,6 +163,7 @@ class User(Client, EntityBase):
"""
UPDATABLE = ['name', 'email', 'uco', 'gitlab_username', 'managed']
BASE_PARAMS = ['username', 'codename', 'is_admin', *UPDATABLE, 'managed', 'id']
LISTABLE = ['username', 'gitlab_username']
__tablename__ = 'user'
id = db.Column(db.String(length=36), db.ForeignKey('client.id'),
default=lambda: str(uuid.uuid4()), primary_key=True)
......@@ -174,7 +174,7 @@ class User(Client, EntityBase):
# optional - after password change
password_hash = db.Column(db.String(120), default=None)
gitlab_username = db.Column(db.String(50), nullable=True)
_gitlab_username = db.Column('gitlab_username', db.String(50), nullable=True)
managed = db.Column(db.Boolean, default=False)
submissions = db.relationship("Submission", back_populates="user", cascade="all, delete-orphan",
passive_deletes=True)
......@@ -185,11 +185,19 @@ class User(Client, EntityBase):
}
@hybrid_property
def username(self):
def username(self) -> str:
return self.codename
@hybrid_property
def gitlab_username(self) -> str:
return self._gitlab_username or self.username
@gitlab_username.setter
def gitlab_username(self, value: str):
self._gitlab_username = value
@username.setter
def username(self, value):
def username(self, value: str):
self.codename = value
def is_self(self, eid):
......@@ -276,7 +284,7 @@ class User(Client, EntityBase):
return self.query_projects_by_course(course=course).all()
def __init__(self, uco: int = None, email: str = None, username: str = None,
is_admin: bool = False, **kwargs) -> None:
is_admin: bool = False, gitlab_username: str = None, **kwargs) -> None:
"""Creates user instance
Args:
uco(int): User's UCO (school identifier)
......@@ -288,6 +296,7 @@ class User(Client, EntityBase):
self.email = email
self.username = username
self.is_admin = is_admin
self.gitlab_username = gitlab_username
super().__init__(ClientType.USER, **kwargs)
def __eq__(self, other):
......
......@@ -84,9 +84,8 @@ class GitlabFacade(GeneralFacade):
log.debug(f"[GL_LOGIN] User not found: {user_info.username}")
self.user_oauth_register(user_info)
log.debug(f"[GL_LOGIN] User found: {user_info.username}")
resp = self._make_response(token=token, token_type=token_type, user_info=user_info,
redir=redir)
return resp
params = self._extracted_params(token=token, token_type=token_type, user_info=user_info,)
return flask.jsonify(params) if not redir else self._make_login_response(params)
def user_oauth_register(self, user_info: objects.CurrentUser) -> User:
new_user = self.services.users.create(
......@@ -101,16 +100,16 @@ class GitlabFacade(GeneralFacade):
log.info(f"[GITLAB] Created user after GL login: {new_user}")
return new_user
def _make_response(self, token, token_type='oauth', user_info=None, redir=True):
def _extracted_params(self, user_info, token, token_type):
params = {
'gitlab_token': token,
'gitlab_token_type': token_type,
'gitlab_username': user_info.username,
'username': user_info.username,
'gitlab_url': self._config.get('GITLAB_URL')
'gitlab_url': self._config.get('GITLAB_URL'),
'gitlab_domain': self._config.get('GITLAB_BASE_DOMAIN')
}
return flask.jsonify(params) if not redir else self._make_login_response(params)
return params
def _make_login_response(self, params):
frontend = self._config.get('FRONTEND_URL') or 'http://localhost:4200'
......
......@@ -219,7 +219,7 @@ class SubmissionsService(GeneralService):
def __get_default_git_url(self, project: Project, user: User):
git_base = self._config.get('GIT_REPO_BASE', 'git@gitlab.fi.muni.cz')
return f"{git_base}:{user.username}/{project.course.codename}.git"
return f"{git_base}:{user.gitlab_username}/{project.course.codename}.git"
def get_stats(self):
"""Gets the statistics for the submission
......
......@@ -231,9 +231,10 @@ def test_set_gitlab_service_cookies(client, gl_api_url, gl_user, gl_cookies):
response = rest_tools.make_request(client, f'/gitlab/service_token')
assert_response(response)
res_proj = rest_tools.extract_data(response)
assert len(res_proj) == 5
assert len(res_proj) == 6
assert res_proj['username'] == gl_cookies['username']
assert res_proj['gitlab_username'] == gl_cookies['gitlab_username']
assert res_proj['gitlab_token'] == gl_cookies['gitlab_token']
assert res_proj['gitlab_token_type'] == gl_cookies['gitlab_token_type']
assert res_proj['gitlab_url']
assert res_proj['gitlab_domain']
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment