2015-05-14 20:47:38 +00:00
|
|
|
""" Authorize repository to send e-mail notifications. """
|
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from flask import request, abort
|
|
|
|
|
|
|
|
from endpoints.api import (resource, nickname, require_repo_admin, RepositoryParamResource,
|
2014-09-22 23:11:48 +00:00
|
|
|
log_action, validate_json_request, NotFound, internal_only,
|
2014-10-02 19:08:32 +00:00
|
|
|
path_param, show_if)
|
2014-07-28 18:58:12 +00:00
|
|
|
|
|
|
|
from app import tf
|
|
|
|
from data import model
|
|
|
|
from data.database import db
|
|
|
|
from util.useremails import send_repo_authorization_email
|
|
|
|
|
|
|
|
import features
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
def record_view(record):
|
|
|
|
return {
|
|
|
|
'email': record.email,
|
|
|
|
'repository': record.repository.name,
|
2014-09-24 22:01:35 +00:00
|
|
|
'namespace': record.repository.namespace_user.username,
|
2014-07-28 18:58:12 +00:00
|
|
|
'confirmed': record.confirmed
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@internal_only
|
2014-09-22 23:11:48 +00:00
|
|
|
@show_if(features.MAILING)
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/authorizedemail/<email>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('email', 'The e-mail address')
|
2014-07-28 18:58:12 +00:00
|
|
|
class RepositoryAuthorizedEmail(RepositoryParamResource):
|
|
|
|
""" Resource for checking and authorizing e-mail addresses to receive repo notifications. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('checkRepoEmailAuthorized')
|
|
|
|
def get(self, namespace, repository, email):
|
|
|
|
""" Checks to see if the given e-mail address is authorized on this repository. """
|
2015-07-15 21:25:41 +00:00
|
|
|
record = model.repository.get_email_authorized_for_repo(namespace, repository, email)
|
2014-07-28 18:58:12 +00:00
|
|
|
if not record:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return record_view(record)
|
|
|
|
|
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('sendAuthorizeRepoEmail')
|
|
|
|
def post(self, namespace, repository, email):
|
|
|
|
""" Starts the authorization process for an e-mail address on a repository. """
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
with tf(db):
|
2015-07-15 21:25:41 +00:00
|
|
|
record = model.repository.get_email_authorized_for_repo(namespace, repository, email)
|
2014-07-28 18:58:12 +00:00
|
|
|
if record and record.confirmed:
|
|
|
|
return record_view(record)
|
|
|
|
|
|
|
|
if not record:
|
2015-07-15 21:25:41 +00:00
|
|
|
record = model.repository.create_email_authorization_for_repo(namespace, repository, email)
|
2014-07-28 18:58:12 +00:00
|
|
|
|
|
|
|
send_repo_authorization_email(namespace, repository, email, record.code)
|
|
|
|
return record_view(record)
|