__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from pkg_resources import resource_filename
  17. import os
  18. from sqlalchemy import or_
  19. from mediagoblin.auth.tools import create_basic_user
  20. from mediagoblin.db.models import User
  21. from mediagoblin.plugins.persona.models import PersonaUserEmails
  22. from mediagoblin.tools import pluginapi
  23. from mediagoblin.tools.staticdirect import PluginStatic
  24. from mediagoblin.tools.translate import pass_to_ugettext as _
  25. PLUGIN_DIR = os.path.dirname(__file__)
  26. def setup_plugin():
  27. config = pluginapi.get_config('mediagoblin.plugins.persona')
  28. routes = [
  29. ('mediagoblin.plugins.persona.login',
  30. '/auth/persona/login/',
  31. 'mediagoblin.plugins.persona.views:login'),
  32. ('mediagoblin.plugins.persona.register',
  33. '/auth/persona/register/',
  34. 'mediagoblin.plugins.persona.views:register'),
  35. ('mediagoblin.plugins.persona.edit',
  36. '/edit/persona/',
  37. 'mediagoblin.plugins.persona.views:edit'),
  38. ('mediagoblin.plugins.persona.add',
  39. '/edit/persona/add/',
  40. 'mediagoblin.plugins.persona.views:add')]
  41. pluginapi.register_routes(routes)
  42. pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
  43. pluginapi.register_template_hooks(
  44. {'persona_end': 'mediagoblin/plugins/persona/persona_js_end.html',
  45. 'persona_form': 'mediagoblin/plugins/persona/persona.html',
  46. 'edit_link': 'mediagoblin/plugins/persona/edit_link.html',
  47. 'login_link': 'mediagoblin/plugins/persona/login_link.html',
  48. 'register_link': 'mediagoblin/plugins/persona/register_link.html'})
  49. def create_user(register_form):
  50. if 'persona_email' in register_form:
  51. username = register_form.username.data
  52. user = User.query.filter(
  53. or_(
  54. User.username == username,
  55. User.email == username,
  56. )).first()
  57. if not user:
  58. user = create_basic_user(register_form)
  59. new_entry = PersonaUserEmails()
  60. new_entry.persona_email = register_form.persona_email.data
  61. new_entry.user_id = user.id
  62. new_entry.save()
  63. return user
  64. def extra_validation(register_form):
  65. persona_email = register_form.persona_email.data if 'persona_email' in \
  66. register_form else None
  67. if persona_email:
  68. persona_email_exists = PersonaUserEmails.query.filter_by(
  69. persona_email=persona_email
  70. ).count()
  71. extra_validation_passes = True
  72. if persona_email_exists:
  73. register_form.persona_email.errors.append(
  74. _('Sorry, an account is already registered to that Persona'
  75. ' email.'))
  76. extra_validation_passes = False
  77. return extra_validation_passes
  78. def Auth():
  79. return True
  80. def add_to_global_context(context):
  81. if len(pluginapi.hook_runall('authentication')) == 1:
  82. context['persona_auth'] = True
  83. context['persona'] = True
  84. return context
  85. hooks = {
  86. 'setup': setup_plugin,
  87. 'authentication': Auth,
  88. 'auth_extra_validation': extra_validation,
  89. 'auth_create_user': create_user,
  90. 'template_global_context': add_to_global_context,
  91. 'static_setup': lambda: PluginStatic(
  92. 'coreplugin_persona',
  93. resource_filename('mediagoblin.plugins.persona', 'static'))
  94. }