__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. import os
  17. import uuid
  18. from sqlalchemy import or_
  19. from mediagoblin.auth.tools import create_basic_user
  20. from mediagoblin.db.models import User, LocalUser
  21. from mediagoblin.plugins.openid.models import OpenIDUserURL
  22. from mediagoblin.tools import pluginapi
  23. from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
  24. PLUGIN_DIR = os.path.dirname(__file__)
  25. def setup_plugin():
  26. config = pluginapi.get_config('mediagoblin.plugins.openid')
  27. routes = [
  28. ('mediagoblin.plugins.openid.register',
  29. '/auth/openid/register/',
  30. 'mediagoblin.plugins.openid.views:register'),
  31. ('mediagoblin.plugins.openid.login',
  32. '/auth/openid/login/',
  33. 'mediagoblin.plugins.openid.views:login'),
  34. ('mediagoblin.plugins.openid.finish_login',
  35. '/auth/openid/login/finish/',
  36. 'mediagoblin.plugins.openid.views:finish_login'),
  37. ('mediagoblin.plugins.openid.edit',
  38. '/edit/openid/',
  39. 'mediagoblin.plugins.openid.views:start_edit'),
  40. ('mediagoblin.plugins.openid.finish_edit',
  41. '/edit/openid/finish/',
  42. 'mediagoblin.plugins.openid.views:finish_edit'),
  43. ('mediagoblin.plugins.openid.delete',
  44. '/edit/openid/delete/',
  45. 'mediagoblin.plugins.openid.views:delete_openid'),
  46. ('mediagoblin.plugins.openid.finish_delete',
  47. '/edit/openid/delete/finish/',
  48. 'mediagoblin.plugins.openid.views:finish_delete')]
  49. pluginapi.register_routes(routes)
  50. pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
  51. pluginapi.register_template_hooks(
  52. {'register_link': 'mediagoblin/plugins/openid/register_link.html',
  53. 'login_link': 'mediagoblin/plugins/openid/login_link.html',
  54. 'edit_link': 'mediagoblin/plugins/openid/edit_link.html'})
  55. def create_user(register_form):
  56. if 'openid' in register_form:
  57. username = register_form.username.data
  58. user = User.query.filter(
  59. or_(
  60. LocalUser.username == username,
  61. LocalUser.email == username,
  62. )).first()
  63. if not user:
  64. user = create_basic_user(register_form)
  65. new_entry = OpenIDUserURL()
  66. new_entry.openid_url = register_form.openid.data
  67. new_entry.user_id = user.id
  68. new_entry.save()
  69. return user
  70. def extra_validation(register_form):
  71. openid = register_form.openid.data if 'openid' in \
  72. register_form else None
  73. if openid:
  74. openid_url_exists = OpenIDUserURL.query.filter_by(
  75. openid_url=openid
  76. ).count()
  77. extra_validation_passes = True
  78. if openid_url_exists:
  79. register_form.openid.errors.append(
  80. _('Sorry, an account is already registered to that OpenID.'))
  81. extra_validation_passes = False
  82. return extra_validation_passes
  83. def no_pass_redirect():
  84. return 'openid'
  85. def add_to_form_context(context):
  86. context['openid_link'] = True
  87. return context
  88. def Auth():
  89. return True
  90. hooks = {
  91. 'setup': setup_plugin,
  92. 'authentication': Auth,
  93. 'auth_extra_validation': extra_validation,
  94. 'auth_create_user': create_user,
  95. 'auth_no_pass_redirect': no_pass_redirect,
  96. ('mediagoblin.auth.register',
  97. 'mediagoblin/auth/register.html'): add_to_form_context,
  98. ('mediagoblin.auth.login',
  99. 'mediagoblin/auth/login.html'): add_to_form_context
  100. }