def update_user(data): try: simcard = Enduser.get_or_create_simcard(data.get('telephone')) if not simcard: return ( 'Bad Telephone',None) telephone = simcard.msisdn user = Enduser.get_active_user(telephone) #if nid is invalid return if len(data.get('national_id')) != 16: return ( 'Bad National ID',None) national_id = data.get('national_id') old_user = fetch_enduser(national_id, telephone) ## Check national_id registered for the telephone if not old_user: return ( 'User does not exist with NID %s and Telephone %s' % (national_id, telephone) , None) else: if (user and old_user) and old_user.indexcol != user.indexcol: return ( 'User with NID %s and Telephone %s is active, please deactive him/her first.' % ( user.national_id, user.telephone) , None) elif (old_user.indexcol == user.indexcol) or (old_user.indexcol == data.get('indexcol')): # He is the same user, check if credentials have been updated new_passwd = data.get('passwd') if old_user.email != data.get('email') or new_passwd: if new_passwd: generated_password = MchSecurity.generatedPassword(new_passwd) data.update({"salt" : generated_password[0]}) data.update({"passwd" : generated_password[1]}) else: new_passwd = simcard.msin generated_password = MchSecurity.generatedPassword(new_passwd) data.update({"salt" : generated_password[0]}) data.update({"passwd" : generated_password[1]}) data.update({"simcard_pk": simcard.indexcol}) data.update({"telephone": telephone}) #print "SAVE DATA: ", data migrate(Enduser._table, data) user = Enduser.get_active_user( data.get('telephone') ) sent = False if user.role_code == 'BINOME' or user.role_code == 'ASM' or user.language_code == 'RW': sent = Enduser.send_message(telephone, "Twabamenyeshaga ko mumaze gukosorwa kuri list, muri sisitemu ya RapidSMS Rwanda, nka %(role)s, ku rwego rw %(level)s, %(nat)s, Intara %(prv)s, Akarere ka %(dst)s, ibitaro bya %(hp)s ikigo nderabuzima cya %(hc)s, umurenge wa %(sec)s, akagari ka %(cel)s, umudugudu wa %(vil)s. Murakoze."% { 'role': user.role_name, 'hc': user.facility_name, 'sec': user.sector_name, 'cel': user.cell_name , 'vil': user.village_name, 'level': Enduser._levels.get(user.location_level_code)[1], 'dst': user.district_name, 'hp': user.referral_name, 'nat': user.nation_name, 'prv': user.province_name}) else: sent = Enduser.send_message(telephone, "You have been updated in RapidSMS, your username is: %(email)s, and password is: %(pwd)s. Thanks." % {'email': user.email, 'pwd': new_passwd}) if not sent: return ("User updated, but SMS not sent, please contact username: %(email)s, and password: %(pwd)s" % {'email': user.email, 'pwd': data.get('passwd')}, old_user) return ( 'User updated', old_user) else: return ( 'User with NID %s and Telephone %s cannot be updated, contact system administrator.' % ( old_user.national_id, old_user.telephone) , None) except Exception, e: print e pass
def login(self): user = self.get_user() if user: token = MchSecurity.generatedToken(self.usern, self.pwd) ##print "TOKEN: \n", token, "\n", self.usern, "\n", user.__dict__, "\n" else: token = None return token
def auth_facilities(self): try: ##print "FAC: ", len(self.facilities) tot = fetch_table_cols('facility', {}, cols = ['COUNT(*) AS value'])[0] if len(self.facilities) != tot.value: self.facilities = fetch_facilities() if self.facilities: KEYS = MchSecurity.get_user_facility_level_filter_keys(self.user) ##print "KEYS: ", KEYS FACS = UNDERSCORE(self.facilities).chain().filter(lambda x, *args: getattr(x, KEYS[0]) == KEYS[1] ).indexBy(lambda x, *args: x.indexcol).map(lambda x, *args: x).sortBy().value() ##print "AUTH FACS: ", len(FACS) return FACS ##print "TURI KUHARENGA" else: pass return MchSecurity.get_auth_facilities(self.user) except Exception, e: #print "NO FACILITIES ASSIGNED TO USER : %s" % self.user.usern pass
def reset_password(email): try: user, otp, token = MchSecurity.get_otp(email) #print "OTP: ", otp sent = Enduser.send_message( user.telephone, "Your OTP to change password , now, is: %s" % otp) return (token, sent) except Exception, e: print e
def __init__(self, auth, *args, **kw): self.args = args self.kw = kw self.auth = auth td = datetime.today() self.sta = datetime(year = td.year, month = td.month, day = td.day, hour = 0, minute = 0, second = 0, microsecond = 0) self.fin = datetime(year = td.year, month = td.month, day = td.day, hour = td.hour, minute = td.minute, second = td.second, microsecond = td.microsecond) self.gap = timedelta(days = 1000 - 1) self.privileges = MchSecurity.get_privileges() self.auth_pages = {}
def change_password(email, tkn, otp, new_passwd): message = None if tkn and otp: try: seen = MchSecurity.verify_otp(tkn, otp) #;print "SEEN: ", seen if seen: #print "SEEN: ", otp, new_passwd user = MchSecurity.get_user_by_email(email) formdata = { "indexcol": user.indexcol, "telephone": user.telephone, "national_id": user.national_id, "email": email, "passwd": new_passwd } #print "\nFORM: ", formdata, "\n" message, user = Enduser.update_user(formdata) return (message, True) except Exception, e: print e message = e
def conditions(self, tn = None, ini = None, cols = []): ans = ini.conditions() if ini else {} if tn and tn in cols: ans.update({ (tn + ' >= %s') : self.start, (tn + ' <= %s') : self.finish }) if self.auth.user: lvl = MchSecurity.get_auth_location(self.auth.user, self.kw) if lvl.get('sector_pk'): ans.update({'sector_pk = %s': lvl.get('sector_pk')}) if lvl.get('cell_pk'): ans.update({'cell_pk = %s': lvl.get('cell_pk')}) if lvl.get('village_pk'): ans.update({'village_pk = %s': lvl.get('village_pk')}) if lvl.get('facility_pk'): ans.update({'facility_pk = %s': lvl.get('facility_pk')}) if lvl.get('referral_facility_pk'): ans.update({'referral_facility_pk = %s': lvl.get('referral_facility_pk')}) if lvl.get('district_pk'): ans.update({'district_pk = %s': lvl.get('district_pk')}) if lvl.get('province_pk'): ans.update({'province_pk = %s': lvl.get('province_pk')}) else: pass return ans
def get_or_create( data): try: simcard = Enduser.get_or_create_simcard(data.get('telephone')) if not simcard: return ( 'Bad Telephone',None) telephone = simcard.msisdn user = Enduser.get_active_user(telephone) if not user: if len(data.get('national_id')) != 16: return ( 'Bad National ID',None) if not data.get('email'): data.update({"email": '*****@*****.**' % simcard.indexcol}) data.update({"simcard_pk": simcard.indexcol}) data.update({"telephone": telephone}) generated_password = MchSecurity.generatedPassword(simcard.msin) data.update({"salt" : generated_password[0]}) data.update({"passwd" : generated_password[1]}) national_id = data.get('national_id') #print "SAVE DATA: ", data migrate(Enduser._table, data) #user = fetch_enduser(national_id, telephone) user = Enduser.get_active_user( telephone ) sent = False if user.role_code == 'BINOME' or user.role_code == 'ASM' or user.language_code == 'RW': sent = Enduser.send_message(telephone, "Twabamenyeshaga ko mumaze kwandikwa kuri list, muri sisitemu ya RapidSMS Rwanda, nka %(role)s, ku rwego rw %(level)s, %(nat)s, Intara %(prv)s, Akarere ka %(dst)s, ibitaro bya %(hp)s ikigo nderabuzima cya %(hc)s, umurenge wa %(sec)s, akagari ka %(cel)s, umudugudu wa %(vil)s. Murakoze."% { 'role': user.role_name, 'hc': user.facility_name, 'sec': user.sector_name, 'cel': user.cell_name , 'vil': user.village_name, 'level': Enduser._levels.get(user.location_level_code)[1], 'dst': user.district_name, 'hp': user.referral_name, 'nat': user.nation_name, 'prv': user.province_name}) else: sent = Enduser.send_message(telephone, "You have been registered in RapidSMS as %(role)s, your username is: %(email)s, and password is: %(pwd)s. Thanks." % {'role': user.role_name, 'email': user.email, 'pwd': simcard.msin}) email = Enduser.send_email("RapidSMS Credentials", user.email, "You have been registered in RapidSMS as %(role)s, your username is: %(email)s, and password is: %(pwd)s. Thanks." % {'role': user.role_name, 'email': user.email, 'pwd': simcard.msin}) if not sent: return ("User Created, but SMS not sent, please contact username: %(email)s, and password: %(pwd)s" % {'email': user.email, 'pwd': simcard.msin}, user) return ( 'User created', user) return ( 'User exists', user) except Exception, e: print e pass
def auth_filter_locations(self, loctype, locparentid): self.user = self.get_user() return MchSecurity.get_auth_filter_locations(self.user, loctype, locparentid)
def auth_villages(self): return MchSecurity.get_auth_villages(self.user)
def auth_pages(self, user): return MchSecurity.get_auth_pages(user)
def get_user(self): user = MchSecurity.authenticateUser(email = self.usern, passwd = self.pwd, token = self.token) return user