Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Ceiba/Qt: Deprecate role param in ceiba.login; use sso_login(bool) to…
Browse files Browse the repository at this point in the history
… tell different login methods
  • Loading branch information
jameshwc committed Feb 28, 2022
1 parent b195801 commit 8b9dc2a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 35 deletions.
20 changes: 11 additions & 9 deletions ceiba/ceiba.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,32 @@ def login_user(self, username, password):
if any(x in resp.content.decode('utf-8') for x in ['登入失敗', '更改密碼']):
raise InvalidCredentials
resp = util.post(self.sess, resp.url, data=payload) # idk why it needs to post twice
self.role = util.homepage_url_to_role(resp.url, sso=True)

def login_alternative_user(self, username: str, password: str):
payload = {'loginid': username, 'password': password, 'op': 'login'}
resp = util.post(self.sess, util.login_alternative_url, data=payload) # will get resp that redirect to /ChkSessLib.php
if '登出' not in resp.content.decode('utf-8'):
raise InvalidCredentials
self.role = util.homepage_url_to_role(resp.url)

def login(self, role: int = 0,
def login(self, sso_login=True,
cookie_PHPSESSID: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
progress = None):
'''
:param role: Integer. 0: Student (NTU), 1: TA, 2: Professor, 3: Outside NTU
'''
try:
self.role = Role(role)
except ValueError:
raise InvalidLoginRole

if cookie_PHPSESSID:
self.sess.cookies.set("PHPSESSID", cookie_PHPSESSID)
if sso_login:
resp = self.sess.get(util.login_url)
else:
resp = self.sess.get(util.login_alternative_url)
self.role = util.homepage_url_to_role(resp.url, sso=sso_login)
if self.role == None:
raise InvalidLoginParameters # TODO: make the exception match for the cause
elif username and password:
if self.role.is_sso_login:
if sso_login:
self.login_user(username, password)
else:
self.login_alternative_user(username, password)
Expand Down
21 changes: 10 additions & 11 deletions ceiba/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ def __str__(self):
def is_admin(self) -> bool:
return self == Role.TA or self == Role.Professor or self == Role.Outside_Teacher

@property
def is_sso_login(self) -> bool: # Use username/password set up in Ceiba
return self == Role.Student or self == Role.Professor


class String:

def __init__(self):
Expand Down Expand Up @@ -84,6 +79,14 @@ def course(self, course_name: str, course_num: str, class_num: str) -> str:
return self._data['course'][self.lang].format(course_name, course_num, class_num)
return self._data['course_without_class'][self.lang].format(course_name, course_num)

@property
def sso_login(self) -> str:
return self._data['sso_login'][self.lang]

@property
def alternative_login(self) -> str:
return self._data['alternative_login'][self.lang]

@property
def cancel_on_object(self) -> str:
return self._data['cancel_on_object'][self.lang]
Expand Down Expand Up @@ -281,11 +284,7 @@ def qt_resume_button(self) -> str:
return self._data['qt_resume_button'][self.lang]

@property
def qt_role_label(self) -> str:
return self._data['qt_role_label'][self.lang]

@property
def qt_role_menu_placeholder(self) -> str:
return self._data['qt_role_menu_placeholder'][self.lang]
def qt_login_method_label(self) -> str:
return self._data['qt_login_method_label'][self.lang]

strings = String()
5 changes: 3 additions & 2 deletions ceiba/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"info": {
"course": "{} {} (Class {})",
"course_without_class": "{} {}",
"sso_login": "NTU faculty, staff, & students",
"alternative_login": "TAs, outside instructors & students, auditors, etc.",
"cancel_on_object": "There is no {1} module in course {0}! Cancel the download of {}.",
"wrong_with_object": "Error when downloading {0}! Cancel the download of {2} in {1}.",
"wrong_with_downloading_url": "Error when downloading {} ! Url: {}",
Expand Down Expand Up @@ -62,7 +64,6 @@
"qt": {
"pause_button": "Pause Download",
"resume_button": "Resume Download",
"role_label": "Role: ",
"role_menu_placeholder": "Please select your login role"
"login_method_label": "Login Method:"
}
}
5 changes: 3 additions & 2 deletions ceiba/i18n/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"info": {
"course": "{} {} ({} 班)",
"course_without_class": "{} {}",
"sso_login": "計中帳號登入",
"alternative_login": "助教、校外師生、旁聽生登入",
"cancel_on_object": "{} 的 {} 功能並未開啟!取消下載 {} 檔案。",
"wrong_with_object": "下載 {} 時遇到問題!取消下載 {} 的 {} 檔案",
"wrong_with_downloading_url": "下載 {} 時遇到問題!網址為 {}",
Expand Down Expand Up @@ -62,7 +64,6 @@
"qt": {
"pause_button": "暫停下載",
"resume_button": "繼續下載",
"role_label": "身分:",
"role_menu_placeholder": "請選擇您的身分"
"login_method_label": "登入方式:"
}
}
18 changes: 17 additions & 1 deletion ceiba/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@
admin_skip_mod = ['calendar', 'user', 'theme', 'grade', 'mail']
admin_mod_num = len(admin_cname_map) - len(admin_skip_mod)

ticket_url = 'https://xk4axzhtgc.execute-api.us-east-2.amazonaws.com/Practicing/message'

def homepage_url_to_role(url: str, sso=False) -> Role:
m = re.match('https:\/\/ceiba\.ntu\.edu\.tw\/([A-Za-z].*)\/', url)
if m and m.group(1):
if m.group(1) == 'student':
if sso:
return Role.Student
return Role.Outside_Student
elif m.group(1) == 'teacher':
return Role.Professor
elif m.group(1) == 'ta':
return Role.TA
elif m.group(1) == 'outside_teacher':
return Role.Outside_Teacher
return None

def skip_href_texts(mod: str, admin: bool):
if admin:
return admin_skip_href_texts(mod)
Expand All @@ -89,7 +106,6 @@ def admin_skip_href_texts(mod: str):
else:
return []

ticket_url = 'https://xk4axzhtgc.execute-api.us-east-2.amazonaws.com/Practicing/message'

def get_valid_filename(name: str) -> str:
s = str(name).strip().replace(' ', '_').replace('/', '-')
Expand Down
18 changes: 8 additions & 10 deletions gui_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,9 @@ def create_login_group_box(self):

self.role_label = QLabel("")
self.login_user_menu = QComboBox()
self.login_user_menu.setPlaceholderText(strings.qt_role_menu_placeholder)
self.login_user_menu.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum)
for role in Role:
self.login_user_menu.addItem(strings.role(role))
self.login_user_menu.addItem(strings.sso_login)
self.login_user_menu.addItem(strings.alternative_login)
self.login_button = QPushButton()
self.login_button.clicked.connect(self.login)

Expand Down Expand Up @@ -337,14 +336,14 @@ def login(self):
if self.method_toggle.isChecked():
worker = Worker(self.ceiba.login, progress=True,
cookie_PHPSESSID=self.password_edit.text(),
role=self.login_user_menu.currentIndex()
sso_login=(self.login_user_menu.currentText() == strings.sso_login)
)
self.progress_bar.setMaximum(1)
else:
worker = Worker(self.ceiba.login, progress=True,
username=self.username_edit.text(),
password=self.password_edit.text(),
role=self.login_user_menu.currentIndex()
sso_login=(self.login_user_menu.currentText() == strings.sso_login)
)
self.progress_bar.setMaximum(2)

Expand Down Expand Up @@ -642,9 +641,8 @@ def open_check_for_updates_msgbox(self, has_new_version: bool):
def set_lang(self, lang: str):
self.ceiba.set_lang(lang)
self.language = lang
for role in Role:
self.login_user_menu.setItemText(role.value, strings.role(role))
self.login_user_menu.setPlaceholderText(strings.qt_role_menu_placeholder)
self.login_user_menu.setItemText(0, strings.sso_login)
self.login_user_menu.setItemText(1, strings.alternative_login)

for i in range(len(self.courses_checkboxes)):
course = self.courses[i]
Expand All @@ -658,7 +656,7 @@ def set_lang(self, lang: str):
else:
self.pause_button.setText(strings.qt_pause_button)

self.role_label.setText(strings.qt_role_label)
self.role_label.setText(strings.qt_login_method_label)

def set_en(self):
self.set_lang('en')
Expand Down Expand Up @@ -709,7 +707,7 @@ def set_zh_tw(self):
self.password_label_text = '密碼 :'

self.login_button.setText('登入')
self.login_method_left_label.setText('登入方式:帳號 / 密碼 [?]')
self.login_method_left_label.setText('認證方式:帳號 / 密碼 [?]')
self.login_method_right_label.setText('cookies [?]')
self.login_method_left_label.setToolTip('除非你信任本程式作者,否則不應該在計中網站以外的地方輸入自己的帳密!')
self.login_method_right_label.setToolTip('透過手動登入 Ceiba 可以從瀏覽器的 F12 視窗看到 Cookies,請複製 PHPSESSID 的內容')
Expand Down

0 comments on commit 8b9dc2a

Please sign in to comment.