auth-tokens: extended views to allowed override of adding scope in EE edition.

This commit is contained in:
Marcin Kuzminski 2017-03-03 17:54:07 +01:00
parent 2a652f5bc7
commit 69e4e26553
2 changed files with 90 additions and 18 deletions

View file

@ -34,13 +34,18 @@ log = logging.getLogger(__name__)
class MyAccountView(BaseAppView):
ALLOW_SCOPED_TOKENS = False
"""
This view has alternative version inside EE, if modified please take a look
in there as well.
"""
def load_default_context(self):
c = self._get_local_tmpl_context()
c.auth_user = self.request.user
c.user = c.auth_user.get_instance()
c.allow_scoped_tokens = self.ALLOW_SCOPED_TOKENS
self._register_global_c(c)
return c
@ -55,8 +60,6 @@ class MyAccountView(BaseAppView):
c = self.load_default_context()
c.active = 'auth_tokens'
show_expired = True
c.lifetime_values = [
(str(-1), _('forever')),
(str(5), _('5 minutes')),
@ -70,9 +73,13 @@ class MyAccountView(BaseAppView):
for x in AuthTokenModel.cls.ROLES]
c.role_options = [(c.role_values, _("Role"))]
c.user_auth_tokens = AuthTokenModel().get_auth_tokens(
c.user.user_id, show_expired=show_expired)
c.user.user_id, show_expired=True)
return self._get_template_context(c)
def maybe_attach_token_scope(self, token):
# implemented in EE edition
pass
@LoginRequired()
@NotAnonymous()
@CSRFRequired()
@ -86,10 +93,12 @@ class MyAccountView(BaseAppView):
description = self.request.POST.get('description')
role = self.request.POST.get('role')
AuthTokenModel().create(c.user.user_id, description, lifetime, role)
token = AuthTokenModel().create(
c.user.user_id, description, lifetime, role)
self.maybe_attach_token_scope(token)
Session().commit()
h.flash(_("Auth token successfully created"), category='success')
h.flash(_("Auth token successfully created"), category='success')
return HTTPFound(h.route_path('my_account_auth_tokens'))
@LoginRequired()

View file

@ -6,7 +6,6 @@
<p>
${_('Each token can have a role. Token with a role can be used only in given context, '
'e.g. VCS tokens can be used together with the authtoken auth plugin for git/hg/svn operations only.')}
${_('Additionally scope for VCS type token can narrow the use to chosen repository.')}
</p>
<table class="rctable auth_tokens">
%if c.user_auth_tokens:
@ -70,7 +69,16 @@
${h.text('description', placeholder=_('Description'))}
${h.select('lifetime', '', c.lifetime_options)}
${h.select('role', '', c.role_options)}
% if c.allow_scoped_tokens:
${h.hidden('scope_repo_id')}
% else:
${h.select('scope_repo_id_disabled', '', ['Scopes available in EE edition'], disabled='disabled')}
% endif
</div>
<p class="help-block">
${_('Repository scope works only with tokens with VCS type.')}
</p>
</div>
<div class="buttons">
${h.submit('save',_('Add'),class_="btn")}
@ -82,14 +90,69 @@
</div>
</div>
</div>
<script>
$(document).ready(function(){
var select2Options = {
'containerCssClass': "drop-menu",
'dropdownCssClass': "drop-menu-dropdown",
'dropdownAutoWidth': true
};
$("#lifetime").select2(select2Options);
$("#role").select2(select2Options);
});
</script>
<script>
$(document).ready(function(){
var select2Options = {
'containerCssClass': "drop-menu",
'dropdownCssClass': "drop-menu-dropdown",
'dropdownAutoWidth': true
};
$("#lifetime").select2(select2Options);
$("#role").select2(select2Options);
var repoFilter = function(data) {
var results = [];
if (!data.results[0]) {
return data
}
$.each(data.results[0].children, function() {
// replace name to ID for submision
this.id = this.obj.repo_id;
results.push(this);
});
data.results[0].children = results;
return data;
};
$("#scope_repo_id_disabled").select2(select2Options);
$("#scope_repo_id").select2({
cachedDataSource: {},
minimumInputLength: 2,
placeholder: "${_('repository scope')}",
dropdownAutoWidth: true,
containerCssClass: "drop-menu",
dropdownCssClass: "drop-menu-dropdown",
formatResult: formatResult,
query: $.debounce(250, function(query){
self = this;
var cacheKey = query.term;
var cachedData = self.cachedDataSource[cacheKey];
if (cachedData) {
query.callback({results: cachedData.results});
} else {
$.ajax({
url: "${h.url('repo_list_data')}",
data: {'query': query.term},
dataType: 'json',
type: 'GET',
success: function(data) {
data = repoFilter(data);
self.cachedDataSource[cacheKey] = data;
query.callback({results: data.results});
},
error: function(data, textStatus, errorThrown) {
alert("Error while fetching entries.\nError code {0} ({1}).".format(data.status, data.statusText));
}
})
}
})
});
});
</script>