def __before__(self): "before" if 'theme' not in session: session['theme'] = '' basedir = config.get('baruwa.themes.base', None) if basedir: # Default theme defaultdir = os.path.join(basedir, 'templates', 'default') if os.path.exists(defaultdir): session['theme'] = 'default' # Host theme themedir = os.path.join(basedir, 'templates', request.server_name) if os.path.exists(themedir): session['theme'] = request.server_name session.save() self.theme = session.get('theme') if 'lang' in session: set_lang(session['lang']) else: try: languages = [lang.split('-')[0] for lang in request.languages if check_language(lang.split('-')[0])] set_lang(languages) except AttributeError: default_lang = config.get('baruwa.default.language', 'en') if check_language(default_lang): set_lang([default_lang]) else: set_lang(['en']) # pylint: disable-msg=W0201 self.invalidate = request.GET.get('uc', None) self.langchange = request.GET.get('lc', None)
def __before__(self): "before" if 'theme' not in session: session['theme'] = '' basedir = config.get('baruwa.themes.base', None) if basedir: # Default theme defaultdir = os.path.join(basedir, 'templates', 'default') if os.path.exists(defaultdir): session['theme'] = 'default' # Host theme themedir = os.path.join(basedir, 'templates', request.server_name) if os.path.exists(themedir): session['theme'] = request.server_name session.save() self.theme = session.get('theme') if 'lang' in session: set_lang(session['lang']) else: try: languages = [ lang.split('-')[0] for lang in request.languages if check_language(lang.split('-')[0]) ] set_lang(languages) except AttributeError: default_lang = config.get('baruwa.default.language', 'en') if check_language(default_lang): set_lang([default_lang]) else: set_lang(['en']) # pylint: disable-msg=W0201 self.invalidate = request.GET.get('uc', None) self.langchange = request.GET.get('lc', None)
def set_language(self): "Set the language" nextpage = request.params.get('next', None) if not nextpage: nextpage = request.headers.get('Referer', None) if not nextpage: nextpage = '/' if '://' in nextpage: from_url = urlparse(nextpage) nextpage = from_url[2] lang_code = request.params.get('language', None) if lang_code and check_language(lang_code): session['lang'] = lang_code session.save() params = [] for param in request.params: if param not in ['language', 'amp']: value = request.params[param] if value: if (param == 'came_from' and '://' in urllib2.unquote(value)): urlparts = urlparse(urllib2.unquote(value)) value = urlparts[2] or '/' params.append('%s=%s' % (urllib2.quote(param), urllib2.quote(value))) if 'lc=1' not in params: params.append('lc=1') if params: nextpage = "%s?%s" % (nextpage, '&'.join(params)) redirect(nextpage)
def set_language(self): "Set the language" nextpage = request.params.get('next', None) if not nextpage: nextpage = request.headers.get('Referer', None) if not nextpage: nextpage = '/' if '://' in nextpage: from_url = urlparse(nextpage) nextpage = from_url[2] lang_code = request.params.get('language', None) if lang_code and check_language(lang_code): session['lang'] = lang_code session.save() params = [] for param in request.params: if not param in ['language', 'amp']: value = request.params[param] if value: if (param == 'came_from' and '://' in urllib2.unquote(value)): urlparts = urlparse(urllib2.unquote(value)) value = urlparts[2] or '/' params.append('%s=%s' % (urllib2.quote(param), urllib2.quote(value))) if 'lc=1' not in params: params.append('lc=1') if params: nextpage = "%s?%s" % (nextpage, '&'.join(params)) redirect(nextpage)
def __before__(self): "before" if 'lang' in session: set_lang(session['lang']) else: try: languages = [lang.split('-')[0] for lang in request.languages if check_language(lang.split('-')[0])] set_lang(languages) except AttributeError: default_lang = config.get('baruwa.default.language', 'en') if check_language(default_lang): set_lang([default_lang]) else: set_lang(['en']) self.invalidate = request.GET.get('uc', None) self.langchange = request.GET.get('lc', None)
def __before__(self): "before" languages = [lang.split('-')[0] for lang in request.languages if check_language(lang.split('-')[0])] set_lang(languages) if 'lang' in session: set_lang(session['lang']) self.invalidate = request.GET.get('uc', None)
def __before__(self): "before" languages = [ lang.split('-')[0] for lang in request.languages if check_language(lang.split('-')[0]) ] set_lang(languages) if 'lang' in session: set_lang(session['lang']) self.invalidate = request.GET.get('uc', None)
def loggedin(self): "Landing page" came_from = (unquote(str(request.params.get('came_from', ''))) or url('/')) if not self.identity: if 'repoze.who.logins' in request.environ: login_counter = request.environ['repoze.who.logins'] + 1 else: abort(409) redirect( url('/accounts/login', came_from=came_from, __logins=login_counter)) userid = self.identity['repoze.who.userid'] user = self.identity['user'] if user is None: try: user, local_part, domain, domains = add_user(userid) msg = _('First time Login from external auth,' ' your local account was created') user_address_update(user, local_part, domain, domains, self.identity) except IntegrityError: Session.rollback() redirect(url('/logout')) except ldap.LDAPError: pass else: if not user.active: redirect(url('/logout')) msg = _('Login successful, Welcome back %(username)s !' % dict(username=userid)) update_login(user) if user.is_peleb: for domain in user.domains: if check_language(domain.language): session['lang'] = domain.language session.save() break session['taskids'] = [] session.save() info = auditmsgs.ACCOUNTLOGIN_MSG % dict(u=user.username) audit_log(user.username, 6, unicode(info), request.host, request.remote_addr, arrow.utcnow().datetime) flash(msg) log.info(msg) redirect(url(came_from))
def loggedin(self): "Landing page" came_from = (unquote(str(request.params.get('came_from', ''))) or url('/')) if not self.identity: if 'repoze.who.logins' in request.environ: login_counter = request.environ['repoze.who.logins'] + 1 else: abort(409) redirect(url('/accounts/login', came_from=came_from, __logins=login_counter)) userid = self.identity['repoze.who.userid'] user = self.identity['user'] if user is None: try: user, local_part, domain, domains = add_user(userid) msg = _('First time Login from external auth,' ' your local account was created') user_address_update(user, local_part, domain, domains, self.identity) except IntegrityError: Session.rollback() redirect(url('/logout')) except ldap.LDAPError: pass else: if not user.active: redirect(url('/logout')) msg = _('Login successful, Welcome back %(username)s !' % dict(username=userid)) update_login(user) if user.is_peleb: for domain in user.domains: if check_language(domain.language): session['lang'] = domain.language session.save() break session['taskids'] = [] session.save() info = auditmsgs.ACCOUNTLOGIN_MSG % dict(u=user.username) audit_log(user.username, 6, unicode(info), request.host, request.remote_addr, arrow.utcnow().datetime) flash(msg) log.info(msg) redirect(url(came_from))
def loggedin(self): "Landing page" came_from = (unquote(str(request.params.get('came_from', ''))) or url('/')) if not self.identity: login_counter = request.environ['repoze.who.logins'] + 1 redirect(url('/accounts/login', came_from=came_from, __logins=login_counter)) userid = self.identity['repoze.who.userid'] user = self.identity['user'] if user is None: try: user = User(username=userid, email=userid) user.active = True local_part, domain = userid.split('@') domains = Session.query(Domain)\ .filter(Domain.name == domain)\ .all() user.domains = domains user.timezone = domains[0].timezone Session.add(user) Session.commit() msg = _('First time Login from external auth,' ' your local account was created') addresses = [] if ('tokens' in self.identity and 'ldap' in self.identity['tokens']): lsettings = Session.query(AuthServer.address, AuthServer.port, LDAPSettings.binddn, LDAPSettings.bindpw, LDAPSettings.usetls)\ .join(LDAPSettings)\ .join(Domain)\ .filter(AuthServer.enabled == True)\ .filter(Domain.name == domain)\ .all() lsettings = lsettings[0] lurl = make_ldap_uri(lsettings.address, lsettings.port) base_dn = get_user_dn(self.identity['tokens'][1]) attributes = ['sn', 'givenName', 'proxyAddresses', 'mail', 'memberOf'] ldapattributes = LDAPAttributes( lurl, base_dn, attributes=attributes, bind_dn=lsettings.binddn, bind_pass=lsettings.bindpw, start_tls=lsettings.usetls ) ldapattributes() attrmap = { 'sn': 'lastname', 'givenName': 'firstname', 'mail': 'email', } update_attrs = False doms = [domains[0].name] doms.extend([alias.name for alias in domains[0].aliases]) for attr in attrmap: if attr == 'mail': for mailattr in ldapattributes[attr]: mailattr = mailattr.lower() if (mailattr != user.email and '@' in mailattr and mailattr.split('@')[1] in doms): address = Address(mailattr) address.user = user addresses.append(address) continue if attr in ldapattributes: setattr(user, attrmap[attr], ldapattributes[attr][0]) update_attrs = True if update_attrs: Session.add(user) Session.commit() # accounts aliases if 'proxyAddresses' in ldapattributes: for mailaddr in ldapattributes['proxyAddresses']: try: if mailaddr.startswith('SMTP:'): continue clean_addr = PROXY_ADDR_RE.sub('', mailaddr) clean_addr = clean_addr.lower() if (mailaddr.startswith('smtp:') and clean_addr.split('@')[1] in doms): # Only add domain if we host it address = Address(clean_addr) address.user = user addresses.append(address) except IndexError: pass # accounts groups if 'memberOf' in ldapattributes: for group_dn in ldapattributes['memberOf']: groupattributes = LDAPAttributes( lurl, group_dn, attributes=['proxyAddresses'], bind_dn=lsettings.binddn, bind_pass=lsettings.bindpw, start_tls=lsettings.usetls ) groupattributes() if 'proxyAddresses' not in groupattributes: continue for mailaddr in groupattributes['proxyAddresses']: try: mailaddr = mailaddr.lower() clean_addr = PROXY_ADDR_RE.sub('', mailaddr) if (mailaddr.startswith('smtp:') and clean_addr.split('@')[1] in doms): address = Address(clean_addr) address.user = user addresses.append(address) except IndexError: pass else: for alias in domains[0].aliases: address = Address('%s@%s' % (local_part, alias.name)) address.user = user addresses.append(address) for unsaved in addresses: try: Session.add(unsaved) Session.commit() except IntegrityError: Session.rollback() except IntegrityError: Session.rollback() redirect(url('/logout')) except ldap.LDAPError: pass else: if not user.active: redirect(url('/logout')) msg = _('Login successful, Welcome back %(username)s !' % dict(username=userid)) user.last_login = now() Session.add(user) Session.commit() if user.is_peleb: for domain in user.domains: if check_language(domain.language): session['lang'] = domain.language session.save() break session['taskids'] = [] session.save() info = ACCOUNTLOGIN_MSG % dict(u=user.username) audit_log(user.username, 6, unicode(info), request.host, request.remote_addr, now()) flash(msg) redirect(url(came_from))
def loggedin(self): "Landing page" came_from = (unquote(str(request.params.get('came_from', ''))) or url('/')) if not self.identity: login_counter = request.environ['repoze.who.logins'] + 1 redirect( url('/accounts/login', came_from=came_from, __logins=login_counter)) userid = self.identity['repoze.who.userid'] user = self.identity['user'] if user is None: try: user = User(username=userid, email=userid) user.active = True local_part, domain = userid.split('@') domains = Session.query(Domain)\ .filter(Domain.name == domain)\ .all() user.domains = domains user.timezone = domains[0].timezone Session.add(user) Session.commit() msg = _('First time Login from external auth,' ' your local account was created') addresses = [] if ('tokens' in self.identity and 'ldap' in self.identity['tokens']): lsettings = Session.query(AuthServer.address, AuthServer.port, LDAPSettings.binddn, LDAPSettings.bindpw, LDAPSettings.usetls)\ .join(Domain)\ .filter(AuthServer.enabled == True)\ .filter(Domain.name == domain)\ .all() lsettings = lsettings[0] lurl = make_ldap_uri(lsettings.address, lsettings.port) base_dn = get_user_dn(self.identity['tokens'][1]) attributes = [ 'sn', 'givenName', 'proxyAddresses', 'mail', 'memberOf' ] ldapattributes = LDAPAttributes(lurl, base_dn, attributes=attributes, bind_dn=lsettings.binddn, bind_pass=lsettings.bindpw, start_tls=lsettings.usetls) ldapattributes() attrmap = { 'sn': 'lastname', 'givenName': 'firstname', 'mail': 'email', } update_attrs = False doms = [domains[0].name] doms.extend([alias.name for alias in domains[0].aliases]) for attr in attrmap: if (attr == 'mail' and attr in ldapattributes and ldapattributes[attr][0] == user.email): # Dont update if user.email = directory.email continue if (attr == 'mail' and attr in ldapattributes and '@' in ldapattributes[attr][0]): # Update if email is hosted by us if ldapattributes[attr][0].split('@')[1] in doms: setattr(user, attrmap[attr], ldapattributes[attr][0]) update_attrs = True continue if attr in ldapattributes: setattr(user, attrmap[attr], ldapattributes[attr][0]) update_attrs = True if update_attrs: Session.add(user) Session.commit() # accounts aliases if 'proxyAddresses' in ldapattributes: for mailaddr in ldapattributes['proxyAddresses']: try: if mailaddr.startswith('SMTP:'): continue if (mailaddr.startswith('smtp:') and mailaddr.strip('smtp:').lsplit('@')[1] in doms): # Only add domain if we host it address = Address( PROXY_ADDR_RE.sub('', mailaddr)) address.user = user addresses.append(address) except IndexError: pass # accounts groups if 'memberOf' in ldapattributes: for group_dn in ldapattributes['memberOf']: groupattributes = LDAPAttributes( lurl, group_dn, attributes=['proxyAddresses'], bind_dn=lsettings.binddn, bind_pass=lsettings.bindpw, start_tls=lsettings.usetls) groupattributes() for mailaddr in groupattributes['proxyAddresses']: try: mailaddr = mailaddr.lower() if (mailaddr.startswith('smtp:') and mailaddr.lstrip('smtp:').split( '@')[1] in doms): address = Address( PROXY_ADDR_RE.sub('', mailaddr)) address.user = user addresses.append(address) except IndexError: pass else: for alias in domains[0].aliases: address = Address('%s@%s' % (local_part, alias.name)) address.user = user addresses.append(address) for unsaved in addresses: try: Session.add(unsaved) Session.commit() except IntegrityError: Session.rollback() except IntegrityError: Session.rollback() redirect(url('/logout')) else: msg = _('Login successful, Welcome back %(username)s !' % dict(username=userid)) user.last_login = now() Session.add(user) Session.commit() if user.is_peleb: for domain in user.domains: if check_language(domain.language): session['lang'] = domain.language session.save() break session['taskids'] = [] session.save() info = ACCOUNTLOGIN_MSG % dict(u=user.username) audit_log(user.username, 6, info, request.host, request.remote_addr, now()) flash(msg) redirect(url(came_from))