def captcha_field(request=request): from gluon.tools import Recaptcha w = lambda x,y: Recaptcha(request, '6LeIK_ISAAAAABlEm6gH7NRcbCLIV_H6e8eEYfvD', '6LeIK_ISAAAAAE-tUY4tCUeAYm5FKizTRroqSHRX') return Field('captcha', 'string', label=T('Verify'), widget=w, default='ok')
def __init__(self, db): self.db = db self.hmac_key = Auth.get_or_create_key() Auth.__init__(self, self.db, hmac_key=self.hmac_key) #self.settings.logout_onlogout = lambda user: remove_session(user) #self.settings.register_onaccept = lambda form: add_to_users_group(form) self.settings.controller = 'person' self.settings.on_failed_authorization = self.url('account', args='not_authorized') self.settings.formstyle = 'divs' self.settings.label_separator = '' self.settings.register_next = self.url('show') self.settings.registration_requires_verification = self.db.config.auth.registration_requires_verification self.settings.registration_requires_approval = self.db.config.auth.registration_requires_approval if 'register' in self.db.request.args and self.db.config.auth.use_recaptcha: from gluon.tools import Recaptcha recaptcha_options = dict( self.db.config.get_list('auth', 'recaptcha')) self.settings.captcha = Recaptcha( self.db.request, recaptcha_options['public'], recaptcha_options['private'], options="theme:'%(theme)s', lang:'%(lang)s'" % recaptcha_options) from datamodel.user import User user = User(self) self.entity = user.entity if self.db.config.auth.server == 'default': self.settings.mailer = Mailer(self.db) else: self.settings.mailer.server = self.db.config.auth.server self.settings.mailer.sender = self.db.config.auth.sender self.settings.mailer.login = self.db.config.auth.login
def __init__(self, db): self.db = db self.hmac_key = Auth.get_or_create_key() Auth.__init__(self, self.db, hmac_key=self.hmac_key) #self.settings.logout_onlogout = lambda user: self.remove_session(user) #self.settings.register_onaccept = lambda form: add_to_users_group(form) self.settings.register_onaccept = [lambda form: self.send_welcome_email(form.vars), lambda form: self.initial_user_permission(form.vars)] #self.settings.login_onaccept = [lambda form: self.initial_user_permission(form.vars)] #self.settings.profile_onvalidation = [] self.settings.profile_onaccept = [lambda form: self.remove_facebook_google_alert(form)] # remove facebook / google alert session #self.settings.change_password_onaccept = [] # send alert email self.settings.controller = 'person' self.settings.allow_basic_login = True self.settings.register_verify_password = True self.settings.login_url = self.url('account', args='login') self.settings.verify_email_next = self.url('account', args='login') self.settings.logged_url = self.url('account', args='profile') self.settings.login_next = self.db.CURL('person', 'show') self.settings.register_next = self.db.CURL('person', 'show') self.settings.profile_next = self.db.CURL('person', 'account', args='profile') self.settings.retrieve_username_next = self.url('account', args='login') self.settings.retrieve_password_next = self.url('account', args='login') self.settings.request_reset_password_next = self.url('account', args='login') self.settings.reset_password_next = self.url('account', args='login') self.settings.change_password_next = self.db.CURL('person', 'show') self.messages.verify_email = \ 'Click on the link http://' + self.db.request.env.http_host + \ self.db.CURL('person', 'account', args=['verify_email']) + \ '/%(key)s to verify your email' self.messages.reset_password = \ 'Click on the link http://' + self.db.request.env.http_host + \ self.db.CURL('person', 'account', args=['reset_password']) + \ '/%(key)s to reset your password' self.settings.on_failed_authorization = self.url('account', args='not_authorized') self.settings.formstyle = 'divs' self.settings.label_separator = '' self.settings.register_next = self.url('show') self.settings.registration_requires_verification = self.db.config.auth.registration_requires_verification self.settings.registration_requires_approval = self.db.config.auth.registration_requires_approval if 'register' in self.db.request.args and self.db.config.auth.use_recaptcha: from gluon.tools import Recaptcha recaptcha_options = dict(self.db.config.get_list('auth', 'recaptcha')) self.settings.captcha = Recaptcha(self.db.request, recaptcha_options['public'], recaptcha_options['private'], options="theme:'%(theme)s', lang:'%(lang)s'" % recaptcha_options) from datamodel.user import User user = User(self) self.entity = user.entity if self.db.config.auth.server == 'default': self.settings.mailer = Mailer(self.db) else: self.settings.mailer.server = self.db.config.auth.server self.settings.mailer.sender = self.db.config.auth.sender self.settings.mailer.login = self.db.config.auth.login
def init_auth(self): """ Auth """ request = current.request settings = self.app_settings auth = Auth(self.db) self.auth = auth auth.settings.hmac_key = settings.security_key # before define_tables() #If I use janrain to login, disable register. if settings.register_method in ['Disabled', 'Janrain']: # disable register auth.settings.actions_disabled.append('register') # If I use Recaptcha to register. if settings.register_method in ['Recaptcha']: auth.settings.captcha = Recaptcha(request, settings.recaptcha_public_key, settings.recaptcha_private_key) self.auth_def() # the auth definition # creates all needed tables auth.define_tables(username=True, migrate=settings.migrate) auth.settings.mailer = self.mail # for user email verification if settings.register_method in ['None', 'Recaptcha', 'Approval']: auth.settings.registration_requires_verification = False else: auth.settings.registration_requires_verification = True if settings.register_method in ['Approval']: auth.settings.registration_requires_approval = True else: auth.settings.registration_requires_approval = False auth.settings.reset_password_requires_verification = True if settings.register_method in ['Janrain']: base_http = 'http://' + str(request.env.http_host) auth.settings.actions_disabled = [ 'register', 'change_password', 'request_reset_password' ] auth.settings.login_form = RPXAccount(request, api_key=settings.janrain_api_key, domain=settings.janrain_domain, url=base_http + \ '/%s/default/user/login' % \ request.application) auth.messages.verify_email = settings.verify_email auth.messages.reset_password = settings.reset_password return auth
def hoi_dap(): forlder_id = cms.get_folder(request.args(0)) hoi_dap = cms.define_table(tablename ='hoi_dap',migrate=True) hoi_dap.folder.writable=False hoi_dap.folder.readable=False hoi_dap.folder.default=forlder_id hoi_dap.avatar.writable=False hoi_dap.avatar.readable=False hoi_dap.avatar.default=' ' hoi_dap.htmlcontent.writable=False hoi_dap.htmlcontent.readable=False from gluon.tools import Recaptcha public_key='6LdtT_YSAAAAALCH4vbHKl1yjqvhB80JZh1J21Lv' private_key='6LdtT_YSAAAAAI6XnBMNNWwSkJeSYtbP_-kW5HUH' ### provided by recaptcha.net from gluon.tools import Crud crud = Crud(cms.db) crud.settings.captcha = Recaptcha(request,public_key,private_key,options = "lang:'en', theme:'clean'") form=crud.create(hoi_dap) # form[0].insert(-1, TR('', Recaptcha(request,public_key,private_key,options = "lang:'en', theme:'clean'"))) if form.process().accepted: from plugin_process import ProcessModel process = ProcessModel() objects = process.define_objects(True) log = process.define_process_log(True) objects_id =objects.insert(folder=forlder_id ,tablename='hoi_dap',table_id=form.vars.id ,process=2) log.insert(objects=objects_id, process=2) response.flash=T('done!') # scr ='''<script type="text/javascript"> # setInterval("location.reload();",1000); # </script>''' # form.append(XML(scr)) # if form.errors: # response.flash=T('Loi nhap du lieu!') # scr ='''<script type="text/javascript"> # setInterval("location.reload();",1000); # </script>''' # form.append(XML(scr)) div = DIV(_id='hoi_dap') div.append(DIV(H2('Nhập câu hỏi',_class="title_box"),_class="tinlienquan")) div.append(form) response.view = 'layout/hoi_dap_13.html' return dict(content=div)
def _(): from gluon.tools import Recaptcha key = 'login_from:%s' % request.env.remote_addr num_login_attempts = cache.ram(key, lambda: 0, None) if num_login_attempts >= MAX_LOGIN_FAILURES: auth.settings.login_captcha = Recaptcha(request, RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY) def login_attempt(form, key=key, n=num_login_attempts + 1): cache.ram(key, lambda n=n: n, 0) def login_success(form, key=key): cache.ram(key, lambda: 0, 0) auth.settings.login_onvalidation.append(login_attempt) auth.settings.login_onaccept.append(login_success)
def form_y_kien_du_thao(): vb_du_thao=request.vars.vbdt div = DIV(_id='hoi_dap') div.append(DIV(H2('Gửi ý kiến đóng góp',_class="title_box"),_class="tinlienquan")) from gluon.tools import Recaptcha public_key='6LdtT_YSAAAAALCH4vbHKl1yjqvhB80JZh1J21Lv' private_key='6LdtT_YSAAAAAI6XnBMNNWwSkJeSYtbP_-kW5HUH' ### provided by recaptcha.net gop_y_du_thao = cms.define_table(tablename ='gop_y_du_thao',migrate=True) gop_y_du_thao.r_gop_y_du_thao.writable=False gop_y_du_thao.r_gop_y_du_thao.readable=False gop_y_du_thao.r_gop_y_du_thao.default=vb_du_thao from gluon.tools import Crud crud = Crud(cms.db) form=crud.create(gop_y_du_thao) form[0].insert(-1, TR('', Recaptcha(request,public_key,private_key,options = "lang:'en', theme:'clean'"))) div.append(form) return div
## - email capabilities ## - authentication (registration, login, logout, ... ) ## - authorization (role based authorization) ## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss) ## - old style crud actions ## (more options discussed in gluon/tools.py) ######################################################################### from gluon.tools import Auth, Crud, Service, PluginManager, prettydate auth = Auth(db, hmac_key=Auth.get_or_create_key()) crud, service, plugins = Crud(db), Service(), PluginManager() ## Enable captcha's :-( from gluon.tools import Recaptcha auth.settings.captcha = Recaptcha(request, '6Lfb_t4SAAAAAB9pG_o1CwrMB40YPsdBsD8GsvlD', '6Lfb_t4SAAAAAGvAHwmkahQ6s44478AL5Cf-fI-x', options="theme:'blackglass'") auth.settings.login_captcha = False auth.settings.retrieve_password_captcha = False #auth.settings.retrieve_username_captcha = False ## create all tables needed by auth if not custom tables db.define_table('courses', Field('course_id', 'string'), migrate=settings.migrate) if db(db.courses.id > 0).isempty(): db.courses.insert(course_id='devcourse') ######################################## db.define_table('auth_user',
# - old style crud actions # (more options discussed in gluon/tools.py) # ------------------------------------------------------------------------- from gluon.tools import Auth, Service, PluginManager, Recaptcha # host names must be a list of allowed host names (glob syntax allowed) auth = Auth(db, host_names=myconf.get('host.names')) service = Service() plugins = PluginManager() # ------------------------------------------------------------------------- # create all tables needed by auth if not custom tables # ------------------------------------------------------------------------- if (myconf.get('staging.level') != 'dev'): auth.settings.captcha = Recaptcha(request, 'PUBLIC_KEY', 'PRIVATE_KEY') auth.define_tables(username=True, signature=False) # ------------------------------------------------------------------------- # configure email # ------------------------------------------------------------------------- mail = auth.settings.mailer mail.settings.server = 'logging' if request.is_local else myconf.get( 'smtp.server') mail.settings.sender = myconf.get('smtp.sender') mail.settings.login = myconf.get('smtp.login') mail.settings.tls = myconf.get('smtp.tls') or False mail.settings.ssl = myconf.get('smtp.ssl') or False # ------------------------------------------------------------------------- # configure auth policy
'smtp.server') mail.settings.sender = upload_conf.take('smtp.sender') mail.settings.login = upload_conf.take('smtp.login') mail.settings.tls = False ## configure auth policy #auth.settings.registration_requires_verification = False #auth.settings.registration_requires_approval = False #auth.settings.reset_password_requires_verification = True # deactive registration of new users auth.settings.actions_disabled.append('register') ## set captchas to be used for registration auth.settings.captcha = Recaptcha(request, upload_conf.take('captchas.public', cast=str), upload_conf.take('captchas.private', cast=str), use_ssl=True) auth.settings.login_captcha = False ## after defining tables, uncomment below to enable auditing # auth.enable_record_versioning(db) ## set SQLite to Write-Ahead-Logging to prevent blocking when one user writes to database try: db.executesql("PRAGMA journal_mode=WAL;") except: pass # add field for all users to store whether they want to be informed via email for every update auth.settings.extra_fields['auth_user'] = [
def initAuth(self): ''' Configuracion de las tablas y parametros de usuario registro y validacion de cuentas ''' db = self.db globals = self.globals request = globals.request T = globals.T #8.1.4 Customizing Auth page 350 self.settings.hmac_key = self.config.AUTH_HMAC_KEY # before define_tables() self.settings.formstyle = 'divs' #format of form if self.config.AUTH_REGIS_METHOD in ['Disabled']: self.settings.actions_disabled.append('register') #disable register #Recaptcha (pag349) if self.config.AUTH_REGIS_METHOD in ['Recaptcha']: self.settings.captcha = Recaptcha(request, self.config.AUTH_RECAP_PUBLKEY, self.config.AUTH_RECAP_PRIVKEY) db.define_table( self.settings.table_user_name, Field('first_name', length=128, default=''), Field('last_name', length=128, default=''), Field('email', length=128, default='', unique=True), Field('password', 'password', length=512, readable=False, label='Password'), Field('registration_key', length=512, writable=False, readable=False, default=''), Field('reset_password_key', length=512, writable=False, readable=False, default=''), Field('registration_id', length=512, writable=False, readable=False, default=''), Field('created_on', 'datetime', default=datetime.datetime.today(), writable=False,readable=False), Field('site_language', length=128,writable=False, readable=False, default='', label=T('Language'))) custom_auth_table = db[self.settings.table_user_name] # get the custom_auth_table custom_auth_table.first_name.requires = IS_NOT_EMPTY(error_message = self.messages.is_empty) custom_auth_table.last_name.requires = IS_NOT_EMPTY(error_message = self.messages.is_empty) custom_auth_table.password.requires = [CRYPT(key = self.settings.hmac_key)] custom_auth_table.email.requires = [ IS_EMAIL(error_message = self.messages.invalid_email), IS_NOT_IN_DB(db, custom_auth_table.email)] self.settings.table_user = custom_auth_table self.define_tables() # creates all needed tables self.settings.mailer = self.mail # for user email verification if self.config.AUTH_REGIS_METHOD in ['None','Recaptcha','Approval']: self.settings.registration_requires_verification = False else: self.settings.registration_requires_verification = True if self.config.AUTH_REGIS_METHOD in ['Approval']: self.settings.registration_requires_approval = True else: self.settings.registration_requires_approval = False self.settings.reset_password_requires_verification = True self.messages.verify_email = 'Click on the link http://'+request.env.http_host+\ URL(r=request,c='default',f='user',args=['verify_email'])+\ '/%(key)s to verify your email' self.messages.reset_password = '******'+request.env.http_host+\ URL(r=request,c='default',f='user',args=['reset_password'])+\ '/%(key)s to reset your password'
def show(): session.publishing=False from blog import Blog, Images from regnews import Regnews from comments import Comments from gluon.tools import Recaptcha Blog(db,ckeditor), Images(db) Regnews(db) Comments(db) images=None if auth.has_membership('administradores') or auth.has_membership('superadministradores'): #logger.debug(session.preview) if not request.args(0): redirect(URL('blog','index')) if session.preview: from blog import Draft Draft(db,ckeditor) if request.args(0).isdigit(): post=db.draft(db.draft.id==request.args(0)) or redirect(URL('blog','index')) else: post=db.draft(db.draft.urlfriendly==request.args(0)) or redirect(URL('blog','index')) else: if request.args(0).isdigit(): post=db.blog(db.blog.id==request.args(0)) or redirect(URL('blog','index')) else: post=db.blog(db.blog.urlfriendly==request.args(0)) or redirect(URL('blog','index')) else: if not request.args(0): redirect(URL('blog','index')) if request.args(0).isdigit(): post=db.blog((db.blog.id==request.args(0)) & (db.blog.public==True) ) or redirect(URL('blog','index')) else: post=db.blog((db.blog.urlfriendly==request.args(0)) & (db.blog.public==True) ) or redirect(URL('blog','index')) if post.image!=None: images=db.images(post.image) #logger.debug(images) comment = Field('comment', 'text', label="Comentario",notnull=True, requires=IS_NOT_EMPTY(error_message="Debes escribir un comentario")) if auth.is_logged_in(): form = SQLFORM.factory(comment, submit_button = 'Comentar', formstyle='bootstrap') else: name = Field('name', 'string', label="Nombre", length=50, requires=IS_NOT_EMPTY(error_message="Debe identificarse con un nombre")) email = Field('email', 'string', length=128, requires=IS_EMAIL(error_message="email incorrecto")) website = Field('website','string', length=128, widget=lambda field,value: SQLFORM.widgets.string.widget(field, value, _placeholder='Opcional')) rnews = Field('rnews', 'bool', default=True) captcha = Recaptcha(request, '6Lf849gSAAAAAD2YgjNJxrelMp7-hPnTNZyyf4pD', '6Lf849gSAAAAAOgabgN9kc5YO1hhEws_un0btbbf',use_ssl=True, error_message='Inválido', label='Verificar:', options='theme:"clean", lang:"es"') # antispam= Field("anti_spam", widget=recaptcha, default='') form = SQLFORM.factory(name, rnews, email, website, comment, submit_button = 'Comentar') form[0].append(captcha) if form.errors.has_key('captcha'): response.flash='Captcha inválido' elif form.validate(keepvalues=False): data=form.vars try: if auth.is_logged_in(): if request.args(0).isdigit(): db.comments.insert(comment=data.comment, user=auth.user.id, blog=request.args(0)) else: db.comments.insert(comment=data.comment, user=auth.user.id, blog=db(db.blog.urlfriendly==request.args(0)).select().first()["id"] ) else: reg=db(db.regnews.email==data.email).select() if len(reg)==0: id=db.regnews.insert(email=data.email, name=data.name, news=data.rnews, website=data.website) if request.args(0).isdigit(): db.comments.insert(comment=data.comment, blog=request.args(0), regnews=id) else: db.comments.insert(comment=data.comment, blog=db(db.blog.urlfriendly==request.args(0)).select().first()["id"], regnews=id) else: if request.args(0).isdigit(): db.comments.insert(comment=data.comment, blog=request.args(0), regnews=reg[0].id) else: db.comments.insert(comment=data.comment, blog=db(db.blog.urlfriendly==request.args(0)).select().first()["id"], regnews=reg[0].id) db.commit() response.flash = "Comentario publicado" #redirect(URL('blog','show', args=request.args(0))) except Exception, ex: logger.debug(ex) db.rollback() response.flash="Su comentario no se pudo enviar. Inténtelo de nuevo"
## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss) ## - old style crud actions ## (more options discussed in gluon/tools.py) ######################################################################### from gluon.tools import Auth, Crud, Service, PluginManager, prettydate, Recaptcha, Mail auth = Auth(db) auth.settings.extra_fields['auth_user'] = [ Field('image', 'upload', requires=IS_EMPTY_OR(IS_IMAGE()), autodelete=True), Field('theme', default='corkboard') ] auth.settings.captcha = Recaptcha(request, '6LfwzNgSAAAAAMmbmBYTtbCCd0EH39aPcT8QZqle', '6LfwzNgSAAAAANZOk80WPMjllGce5az1KfFa08Hc') auth.settings.login_captcha = False ## create a mailer for user verification mail = auth.settings.mailer mail.settings.server = 'smtp.gmail.com:587' mail.settings.sender = '*****@*****.**' mail.settings.login = '******' ## setup auth for email verification/reset auth.settings.registration_requires_verification = True auth.settings.registration_requires_approval = False auth.settings.reset_password_requires_verification = True auth.messages.verify_email = 'Click on the link http://' + request.env.http_host + URL( r=request, c='default', f='user', args=['verify_email']) + '/%(key)s to verify your email' auth.messages.reset_password = '******' + request.env.http_host + URL(
def init_auth(globals, db, config): from gluon.tools import Auth import datetime #8.1.4 Customizing Auth page 350 auth = Auth(globals, db) #authentication/authorization auth.settings.hmac_key = config.register_hmac # before define_tables() if config.register_method in ['Disabled']: auth.settings.actions_disabled.append('register') #disable register #Recaptcha (pag349) if config.register_method in ['Recaptcha']: from gluon.tools import Recaptcha auth.settings.captcha = Recaptcha(request, config.recaptcha_public, config.recaptcha_private) db.define_table(auth.settings.table_user_name, Field('first_name', length=128, default=''), Field('last_name', length=128, default=''), Field('email', length=128, default='', unique=True), Field('password', 'password', length=512, readable=False, label='Password'), Field('registration_key', length=512, writable=False, readable=False, default=''), Field('reset_password_key', length=512, writable=False, readable=False, default=''), Field('registration_id', length=512, writable=False, readable=False, default=''), Field('created_on', 'datetime', default=datetime.datetime.today(), writable=False, readable=False), Field('site_language', length=128, writable=False, readable=False, default='', label=T('Language')), migrate=config.is_first_time) custom_auth_table = db[ auth.settings.table_user_name] # get the custom_auth_table custom_auth_table.first_name.requires = IS_NOT_EMPTY( error_message=auth.messages.is_empty) custom_auth_table.last_name.requires = IS_NOT_EMPTY( error_message=auth.messages.is_empty) custom_auth_table.password.requires = [CRYPT(key=auth.settings.hmac_key)] custom_auth_table.email.requires = [ IS_EMAIL(error_message=auth.messages.invalid_email), IS_NOT_IN_DB(db, custom_auth_table.email) ] auth.settings.table_user = custom_auth_table auth.define_tables() # creates all needed tables auth.settings.mailer = mail # for user email verification if config.register_method in ['None', 'Recaptcha', 'Approval']: auth.settings.registration_requires_verification = False else: auth.settings.registration_requires_verification = True if config.register_method in ['Approval']: auth.settings.registration_requires_approval = True else: auth.settings.registration_requires_approval = False auth.settings.reset_password_requires_verification = True auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+\ URL(r=request,c='default',f='user',args=['verify_email'])+\ '/%(key)s to verify your email' auth.messages.reset_password = '******'+request.env.http_host+\ URL(r=request,c='default',f='user',args=['reset_password'])+\ '/%(key)s to reset your password' return auth