def _create_problem(self, title, pmodid): cur = self.db.cursor() sqlstr = ('INSERT INTO "PROBLEM" ("title", "pmodid", "accessid") ' 'VALUES (%s, %s, %s) RETURNING "proid";') sqlarr = (title, pmodid, 0) cur.execute(sqlstr, sqlarr) proid = None for data in cur: proid = data[0] if proid == None: return None user_idenid = TOJAuth.get_current_iden()['idenid'] with TOJAuth.change_current_iden(self._idendesc): accessid = TOJAuth.instance.create_access(user_idenid) sqlstr = ('UPDATE "PROBLEM" SET "accessid" = %s WHERE "proid" = %s;') sqlarr = (accessid, proid) cur.execute(sqlstr, sqlarr) TOJAuth.instance.set_access_list( accessid, TOJAuth.ROLEID_PROBLEM_ADMIN_GROUP, TOJAuth.ACCESS_ALL ) pmodname = self.get_pmodname_by_pmodid(pmodid) pmod = mod.load_pmod(pmodname) pmod.create_problem_data(proid) return proid
def _create_problem(self, title, pmodid): cur = self.db.cursor() sqlstr = ('INSERT INTO "PROBLEM" ("title", "pmodid", "accessid") ' 'VALUES (%s, %s, %s) RETURNING "proid";') sqlarr = (title, pmodid, 0) cur.execute(sqlstr, sqlarr) proid = None for data in cur: proid = data[0] if proid == None: return None user_idenid = TOJAuth.get_current_iden()['idenid'] with TOJAuth.change_current_iden(self._idendesc): accessid = TOJAuth.instance.create_access(user_idenid) sqlstr = ('UPDATE "PROBLEM" SET "accessid" = %s WHERE "proid" = %s;') sqlarr = (accessid, proid) cur.execute(sqlstr, sqlarr) TOJAuth.instance.set_access_list(accessid, TOJAuth.ROLEID_PROBLEM_ADMIN_GROUP, TOJAuth.ACCESS_ALL) pmodname = self.get_pmodname_by_pmodid(pmodid) pmod = mod.load_pmod(pmodname) pmod.create_problem_data(proid) return proid
def join_square(self, sqid): if( type(sqid) != int ): return 'Eparameter' uid = mod.UserMg.get_current_uid() if uid == None: return 'Euid' sq = self.get_square_info_by_sqid(sqid) if sq == None: return 'Eno_such_sqid' with TOJAuth.change_current_iden(self._idendesc): sqobj = self.load_square(sqid) result = sqobj.join_square(uid) if result == self.JOIN_REJECT: return 'Ereject' elif result == self.JOIN_PENDING: with TOJAuth.change_current_iden(self._idendesc): self._set_user_square_relation(uid, sqid, False) return {'active': False} elif result == self.JOIN_ACCEPT: with TOJAuth.change_current_iden(self._idendesc): self._set_user_square_relation(uid, sqid, True) return {'active': True} else: return 'Ejoin_sq_error'
def get_notice(self, noticeid): cur = self.db.cursor() sqlstr = ('SELECT "noticeid", "uid", "title", "content", "time", ' '"noticemodid", "metadata", "unread" FROM "NOTICE" WHERE ' '"noticeid" = %s;') sqlarr = (noticeid, ) cur.execute(sqlstr, sqlarr) ret = None for data in cur: ret = {} ret['noticeid'] = data[0] ret['uid'] = data[1] ret['title'] = data[2] ret['content'] = data[3] ret['time'] = data[4] ret['noticemodid'] = data[5] ret['metadata'] = data[6] ret['unread'] = data[7] if ret == None: return None uid = mod.UserMg.get_current_uid() if uid != ret['uid']: TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_EXECUTE) return ret
def _create_square(self, title, sqmodid, intro, logo): cur = self.db.cursor() sqlstr = ('INSERT INTO "SQUARE" ("title", "hidden", "sqmodid", ' '"intro", "logo", "accessid") VALUES (%s, %s, %s, %s, ' '%s, %s) RETURNING "sqid";') sqlarr = (title, False, sqmodid, intro, logo, 0) cur.execute(sqlstr, sqlarr) sqid = None for data in cur: sqid = data[0] if sqid == None: return None user_idenid = TOJAuth.get_current_iden()['idenid'] with TOJAuth.change_current_iden(self._idendesc): accessid = TOJAuth.instance.create_access(user_idenid) sqlstr = ('UPDATE "SQUARE" SET "accessid" = %s WHERE "sqid" = %s;') sqlarr = (accessid, sqid) cur.execute(sqlstr, sqlarr) TOJAuth.instance.set_access_list( accessid, TOJAuth.ROLEID_SQUARE_ADMIN_GROUP, TOJAuth.ACCESS_ALL ) sqmodname = self.get_sqmodname_by_sqmodid(sqmodid) sqmod = mod.load_sqmod(sqmodname) sqmod.create_square_data(sqid) return sqid;
def get_user_info_by_uid(self, uid): cur = self.db.cursor() sqlstr = ('SELECT "uid", "username", "nickname", "email", "avatar", ' '"aboutme", "cover" FROM "USER" WHERE "uid" = %s;') sqlarr = (uid, ) cur.execute(sqlstr, sqlarr) ret = None for data in cur: ret = {} ret['uid'] = data[0] ret['username'] = data[1] ret['nickname'] = data[2] ret['email'] = data[3] ret['avatar'] = data[4] ret['aboutme'] = data[5] ret['cover'] = data[6] uid = self.get_current_uid() if uid != ret['uid']: try: TOJAuth.check_access_func( self._accessid, TOJAuth.ACCESS_EXECUTE) except Exception: del ret['email'] return ret
def set_problem(self, proid, title): accessid = self.get_accessid_by_proid(proid) TOJAuth.check_access_func(accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('UPDATE "PROBLEM" SET "title" = %s WHERE "proid" = %s;') sqlarr = (title, proid) cur.execute(sqlstr, sqlarr)
def _set_mode(self, modeid, content, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('UPDATE "PMOD_TEST_MODE" SET "content" = %s, ' '"testmodeid" = %s WHERE "proid" = %s AND "modeid" = %s;') sqlarr = (content, testmodeid, self._proid, modeid) cur.execute(sqlstr, sqlarr)
def _add_testmode(self, testmodename, timelimit, memlimit): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('INSERT INTO "PMOD_TEST_TESTMODE" ("proid", "testmodename", ' '"timelimit", "memlimit") VALUES (%s, %s, %s, %s);') sqlarr = (self._proid, testmodename, timelimit, memlimit) cur.execute(sqlstr, sqlarr)
def _del_mode(self, modeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('DELETE FROM "PMOD_TEST_MODE" WHERE "proid" = %s AND ' '"modeid" = %s;') sqlarr = (self._proid, modeid) cur.execute(sqlstr, sqlarr)
def _set_testmode(self, testmodeid, testmodename, timelimit, memlimit): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('UPDATE "PMOD_TEST_TESTMODE" SET "testmodename" = %s, ' '"timelimit" = %s, "memlimit" = %s WHERE "proid" = %s AND ' '"testmodeid" = %s;') sqlarr = (testmodename, timelimit, memlimit, self._proid, testmodeid) cur.execute(sqlstr, sqlarr)
def set_square(self, sqid, title, start_time, end_time, intro, logo): accessid = self.get_accessid_by_sqid(sqid) TOJAuth.check_access_func(accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('UPDATE "SQUARE" SET "title" = %s, "start_time" = %s, ' '"end_time" = %s, "intro" = %s, "logo" = %s ' 'WHERE "sqid" = %s;') sqlarr = (title, start_time, end_time, intro, logo, sqid) cur.execute(sqlstr, sqlarr)
def _set_square_category(self, sqid, category): sq_accessid = self.get_accessid_by_sqid(sqid) TOJAuth.check_access_func(sq_accessid, TOJAuth.ACCESS_WRITE) if category == []: category = [0] cur = self.db.cursor() sqlstr = ('UPDATE "SQUARE" SET "cateid" = %s WHERE "sqid" = %s;') sqlarr = (category, sqid) cur.execute(sqlstr, sqlarr)
def _list_testdata(self): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) with TOJAuth.change_current_iden(self._idendesc): testdata_list = mod.TestdataMg.instance._list_testdata(self._proid) for test in testdata_list: del test['blobname'] del test['proid'] return testdata_list
def _create_testdata(self, info, filekey, expire): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) # Upload file blobname = 'TEST_BLOBNAME' with TOJAuth.change_current_iden(self._idendesc): testid = mod.TestdataMg.instance._add_testdata( blobname, expire, self._proid, info) return testid
def set_user_info(self, uid, nickname, email, avatar, aboutme, cover): if( type(uid) != int or type(nickname) != str or type(email) != str or type(avatar) != str or type(aboutme) != str or type(cover) != str ): return 'Eparameter' if len(nickname) < self.NICKNAME_LEN_MIN: return 'Enickname_too_short' elif len(nickname) > self.NICKNAME_LEN_MAX: return 'Enickname_too_long' elif len(email) < self.EMAIL_LEN_MIN: return 'Eemail_too_short' elif len(email) > self.EMAIL_LEN_MAX: return 'Eemail_too_long' elif len(avatar) < self.AVATAR_LEN_MIN: return 'Eavatar_too_short' elif len(avatar) > self.AVATAR_LEN_MAX: return 'Eavatar_too_long' elif len(aboutme) < self.ABOUTME_LEN_MIN: return 'Eaboutme_too_short' elif len(aboutme) > self.ABOUTME_LEN_MAX: return 'Eaboutme_too_long' elif len(cover) < self.COVER_LEN_MIN: return 'Ecover_too_short' elif len(cover) > self.COVER_LEN_MAX: return 'Ecover_too_long' idenid = self.get_idenid_by_uid(uid) if idenid == None: return 'Eno_such_uid' if idenid != TOJAuth.get_current_iden()['idenid']: TOJAuth.check_access( self._accessid, TOJAuth.ACCESS_EXECUTE)(lambda x:x)(0) cur = self.db.cursor() sqlstr = ('UPDATE "USER" SET "nickname" = %s, "email" = %s, ' '"avatar" = %s, "aboutme" = %s, "cover" = %s WHERE ' '"uid" = %s;') sqlarr = (nickname, email, avatar, aboutme, cover, uid) cur.execute(sqlstr, sqlarr) return 'Success'
def _unregister_challenge_collector(self,name): link = TOJAuth.get_current_iden(); linkclass = link.split('/')[1] if linkclass != 'backend': return 'Efailed' return self._unregister_collector(link,''.join(['challenge/',name]))
def _send_log(self, data): links = self._client_linkmap.keys() with TOJAuth.change_current_iden(self._idendesc): for link in links: Proxy.instance.call_async(link + 'core/stat/', 'print_log', 10000, None, data)
def get_current_uid(): user_iden = TOJAuth.get_current_iden() try: uid = user_iden['uid'] except KeyError: return None return uid
def _test_get_client_list(self, talk, talk2): stat, ret = Proxy.instance.call( TOJAuth.get_current_iden()['link'] + 'test/route/', '80s', 1000, 'attation', 'mega') print(ret) return list(self._client_linkmap.items())
def set_testdata(self, testid, info, filekey = None, expire = None): if expire != None: expire = com.isoptime(expire) if expire == None: return 'Eparameter' if( type(testid) != int or type(info) != str or (filekey != None and type(filekey) != str) ): return 'Eparameter' with TOJAuth.change_current_iden(self._idendesc): test = mod.TestdataMg.instance._get_testdata(testid) if test == None: return 'Etestid' if test['proid'] != self._proid: return 'Eother_proid' result = self._set_testdata(testid, info, filekey, expire) if result == None: return 'Efailed' if result == False: return 'Eupload' return 'Success'
def _register_status_collector(self,name): link = TOJAuth.get_current_iden() linkclass = link.split('/')[1] if linkclass != 'backend': return 'Efailed' return self._register_collector(link,''.join(['status/',name]))
def __init__(self): super().__init__() self._ioloop = tornado.ioloop.IOLoop.instance() self._pend_filestream_filekeymap = {} self._linkid_usemap = {} self._worker_linkmap = {} self._client_linkmap = {} self._client_backendmap = {} self._backend_workerlist = [] self._uid_clientmap = {} pubkey = open('pubkey.pem', 'r').read() privkey = open('privkey.pem', 'r').read() TOJAuth(pubkey, privkey) self._link = self._create_link('center') self._idendesc = TOJAuth.instance.create_iden(self._link, 1, TOJAuth.ROLETYPE_TOJ) Proxy(self._link, TOJAuth.instance, self._idendesc) self._init_blobserver() Proxy.instance.register_call('core/', 'lookup_link', self._lookup_link) Proxy.instance.register_call('core/', 'create_iden', self._create_iden) Proxy.instance.register_call('core/', 'add_client', self._add_client) Proxy.instance.register_call('core/', 'del_client', self._del_client) Proxy.instance.register_call('core/', 'get_uid_clientlink', self._get_uid_clientlink)
def login(self, username, password): if( type(username) != str or type(password) != str ): return 'Eparameter' uid = self.get_uid_by_username(username) if uid == None: return 'Elogin_failed' passhash = self._password_hash(password) cur = self.db.cursor() sqlstr = ('SELECT "idenid" FROM "USER" WHERE "uid" = %s ' 'AND "passhash" = %s;') sqlarr = (uid, passhash) cur.execute(sqlstr, sqlarr) idenid = None for data in cur: idenid = data[0] if idenid == None: return 'Elogin_failed' client_link = TOJAuth.get_current_iden()['link'] with TOJAuth.change_current_iden(self._idendesc): stat,data = Proxy.instance.call(self.get_link('center') + 'core/', 'create_iden', 10000, client_link, idenid, TOJAuth.ROLETYPE_USER, {'uid' : uid}) if stat == False: return 'Einternal' ret = { 'idendesc' : data, 'uid' : uid, 'hash' : self._uid_passhash_hash(uid, passhash) } return ret
def _add_mode(self, modeid, content, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqltab = ('INSERT INTO "PMOD_TEST_MODE" (') sqlcol = ('"proid", "content", "testmodeid"') sqlval = (') VALUES (%s, %s, %s') sqlend = (');') sqlarr = [self._proid, content, testmodeid] if modeid != None: sqlcol = sqlcol + ', "modeid"' sqlval = sqlval + ', %s' sqlarr.append(modeid) sqlstr = sqltab + sqlcol + sqlval + sqlend cur.execute(sqlstr, sqlarr)
def cookie_login(self, uid, uphash): if( type(uid) != int or type(uphash) != str ): return 'Eparameter' idenid = None real_uphash = None cur = self.db.cursor() sqlstr = ('SELECT "idenid", "passhash" FROM "USER" WHERE "uid" = %s;') sqlarr = (uid, ) cur.execute(sqlstr, sqlarr) for data in cur: idenid = data[0] real_uphash = self._uid_passhash_hash(uid, data[1]) if idenid == None: return 'Elogin_failed' if real_uphash != uphash: return 'Elogin_failed' client_link = TOJAuth.get_current_iden()['link'] with TOJAuth.change_current_iden(self._idendesc): stat,data = Proxy.instance.call(self.get_link('center') + 'core/', 'create_iden', 10000, client_link, idenid, TOJAuth.ROLETYPE_USER, {'uid' : uid}) if stat == False: return 'Einternal' ret = { 'idendesc' : data, 'uid' : uid, 'hash' : uphash } return ret
def get_unseen_count(self): uid = mod.UserMg.get_current_uid() if uid == None: return 'Euid' with TOJAuth.change_current_iden(self._idendesc): unseen_count = self._get_unseen_count(uid) return {'unseen_count': unseen_count}
def _get_mode_by_modeid(self, modeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "proid", "modeid", "content", "testmodeid" FROM ' '"PMOD_TEST_MODE" WHERE "proid" = %s AND "modeid" = %s;') sqlarr = (self._proid, modeid) cur.execute(sqlstr, sqlarr) mode = None for data in cur: mode = {} mode['proid'] = data[0] mode['modeid'] = data[1] mode['content'] = data[2] mode['testmodeid'] = data[3] return mode
def _list_mode(self): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "modeid", "testmodeid" FROM "PMOD_TEST_MODE" ' 'WHERE "proid" = %s ORDER BY "modeid" ASC;') sqlarr = (self._proid, ) cur.execute(sqlstr, sqlarr) mode_list = [] for data in cur: obj = {} obj['modeid'] = data[0] obj['testmodeid'] = data[1] mode_list.append(obj) return mode_list
def _delete_problem(self, proid): accessid = self.get_accessid_by_proid(proid) TOJAuth.check_access_func(accessid, TOJAuth.ACCESS_DELETE) proinfo = self.get_problem_info_by_proid(proid) pmodname = proinfo['pmodname'] pmod = mod.load_pmod(pmodname) with TOJAuth.change_current_iden(self._idendesc): self.unload_problem(proid) pmod.delete_problem_data(proid) TOJAuth.instance.del_access(accessid) cur = self.db.cursor() sqlstr = ('DELETE FROM "PROBLEM" WHERE "proid" = %s;') sqlarr = (proid, ) cur.execute(sqlstr, sqlarr)
def _set_problem_hidden(self, proid, hidden): accessid = self.get_accessid_by_proid(proid) TOJAuth.check_access_func(accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('UPDATE "PROBLEM" SET "hidden" = %s WHERE "proid" = %s;') sqlarr = (hidden, proid) cur.execute(sqlstr, sqlarr) if hidden == True: TOJAuth.instance.set_access_list( accessid, TOJAuth.ROLEID_GUEST, TOJAuth.ACCESS_READ) TOJAuth.instance.set_access_list( accessid, TOJAuth.ROLEID_USER_GROUP, TOJAuth.ACCESS_READ) else: TOJAuth.instance.del_access_list( accessid, TOJAuth.ROLEID_GUEST) TOJAuth.instance.del_access_list( accessid, TOJAuth.ROLEID_USER_GROUP)
def change_user_password(self, uid, old_password, new_password): if( type(uid) != int or type(old_password) != str or type(new_password) != str ): return 'Eparameter' if len(new_password) < self.PASSWORD_LEN_MIN: return 'Epassword_too_short' elif len(new_password) > self.PASSWORD_LEN_MAX: return 'Epassword_too_long' idenid = self.get_idenid_by_uid(uid) if idenid == None: return 'Eno_such_uid' if idenid != TOJAuth.get_current_iden()['idenid']: TOJAuth.check_access( self._accessid, TOJAuth.ACCESS_EXECUTE)(lambda x:x)(0) old_passhash = self._password_hash(old_password) cur = self.db.cursor() sqlstr = ('SELECT "idenid" FROM "USER" WHERE "uid" = %s ' 'AND "passhash" = %s;') sqlarr = (uid, old_passhash) cur.execute(sqlstr, sqlarr) idenid = None for data in cur: idenid = data[0] if idenid == None: return 'Ewrong_old_password' new_passhash = self._password_hash(new_password) sqlstr = ('UPDATE "USER" SET "passhash" = %s WHERE "uid" = %s;') sqlarr = (new_passhash, uid) cur.execute(sqlstr, sqlarr) return 'Success'
def _get_testmode(self, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "testmodeid", "testmodename", "timelimit", ' '"memlimit" FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s AND ' '"testmodeid" = %s;') sqlarr = (self._proid, testmodeid) cur.execute(sqlstr, sqlarr) testmode = None for data in cur: testmode = {} testmode['testmodeid'] = data[0] testmode['testmodename'] = data[1] testmode['timelimit'] = data[2] testmode['memlimit'] = data[3] return testmode
def _set_problem_hidden(self, proid, hidden): accessid = self.get_accessid_by_proid(proid) TOJAuth.check_access_func(accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('UPDATE "PROBLEM" SET "hidden" = %s WHERE "proid" = %s;') sqlarr = (hidden, proid) cur.execute(sqlstr, sqlarr) if hidden == True: TOJAuth.instance.set_access_list(accessid, TOJAuth.ROLEID_GUEST, TOJAuth.ACCESS_READ) TOJAuth.instance.set_access_list(accessid, TOJAuth.ROLEID_USER_GROUP, TOJAuth.ACCESS_READ) else: TOJAuth.instance.del_access_list(accessid, TOJAuth.ROLEID_GUEST) TOJAuth.instance.del_access_list(accessid, TOJAuth.ROLEID_USER_GROUP)
def list_auth(self): uid = self.get_current_uid() if uid == None: return 'Euid' idenid = self.get_idenid_by_uid(uid) with TOJAuth.change_current_iden(self._idendesc): auth_list = TOJAuth.instance.get_user_auth_list(idenid) return auth_list
def register( self, username, password, nickname, email, avatar, aboutme, cover ): if( type(username) != str or type(password) != str or type(nickname) != str or type(email) != str or type(avatar) != str or type(aboutme) != str or type(cover) != str ): return 'Eparameter' if len(username) < self.USERNAME_LEN_MIN: return 'Eusername_too_short' elif len(username) > self.USERNAME_LEN_MAX: return 'Eusername_too_long' elif len(password) < self.PASSWORD_LEN_MIN: return 'Epassword_too_short' elif len(password) > self.PASSWORD_LEN_MAX: return 'Epassword_too_long' elif len(nickname) < self.NICKNAME_LEN_MIN: return 'Enickname_too_short' elif len(nickname) > self.NICKNAME_LEN_MAX: return 'Enickname_too_long' elif len(email) < self.EMAIL_LEN_MIN: return 'Eemail_too_short' elif len(email) > self.EMAIL_LEN_MAX: return 'Eemail_too_long' elif len(avatar) < self.AVATAR_LEN_MIN: return 'Eavatar_too_short' elif len(avatar) > self.AVATAR_LEN_MAX: return 'Eavatar_too_long' elif len(aboutme) < self.ABOUTME_LEN_MIN: return 'Eaboutme_too_short' elif len(aboutme) > self.ABOUTME_LEN_MAX: return 'Eaboutme_too_long' elif len(cover) < self.COVER_LEN_MIN: return 'Ecover_too_short' elif len(cover) > self.COVER_LEN_MAX: return 'Ecover_too_long' passhash = self._password_hash(password) with TOJAuth.change_current_iden(self._idendesc): try: uid = self._create_user( username, passhash, nickname, email, avatar, aboutme, cover ) except psycopg2.IntegrityError: return 'Eusername_exists' return {'uid' : uid}