##// END OF EJS Templates
search: per-repo search shouldn't require admin permissions. Read is enought because we access the repo only
marcink -
r2048:affe5a68 default
parent child Browse files
Show More
@@ -1,133 +1,134 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2011-2017 RhodeCode GmbH
3 # Copyright (C) 2011-2017 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
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/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import logging
21 import logging
22 import urllib
22 import urllib
23 from pyramid.view import view_config
23 from pyramid.view import view_config
24 from webhelpers.util import update_params
24 from webhelpers.util import update_params
25
25
26 from rhodecode.apps._base import BaseAppView, RepoAppView
26 from rhodecode.apps._base import BaseAppView, RepoAppView
27 from rhodecode.lib.auth import (LoginRequired, HasRepoPermissionAnyDecorator)
27 from rhodecode.lib.auth import (LoginRequired, HasRepoPermissionAnyDecorator)
28 from rhodecode.lib.helpers import Page
28 from rhodecode.lib.helpers import Page
29 from rhodecode.lib.utils2 import safe_str, safe_int
29 from rhodecode.lib.utils2 import safe_str, safe_int
30 from rhodecode.lib.index import searcher_from_config
30 from rhodecode.lib.index import searcher_from_config
31 from rhodecode.model import validation_schema
31 from rhodecode.model import validation_schema
32 from rhodecode.model.validation_schema.schemas import search_schema
32 from rhodecode.model.validation_schema.schemas import search_schema
33
33
34 log = logging.getLogger(__name__)
34 log = logging.getLogger(__name__)
35
35
36
36
37 def search(request, tmpl_context, repo_name):
37 def search(request, tmpl_context, repo_name):
38 searcher = searcher_from_config(request.registry.settings)
38 searcher = searcher_from_config(request.registry.settings)
39 formatted_results = []
39 formatted_results = []
40 execution_time = ''
40 execution_time = ''
41
41
42 schema = search_schema.SearchParamsSchema()
42 schema = search_schema.SearchParamsSchema()
43
43
44 search_params = {}
44 search_params = {}
45 errors = []
45 errors = []
46 try:
46 try:
47 search_params = schema.deserialize(
47 search_params = schema.deserialize(
48 dict(search_query=request.GET.get('q'),
48 dict(search_query=request.GET.get('q'),
49 search_type=request.GET.get('type'),
49 search_type=request.GET.get('type'),
50 search_sort=request.GET.get('sort'),
50 search_sort=request.GET.get('sort'),
51 page_limit=request.GET.get('page_limit'),
51 page_limit=request.GET.get('page_limit'),
52 requested_page=request.GET.get('page'))
52 requested_page=request.GET.get('page'))
53 )
53 )
54 except validation_schema.Invalid as e:
54 except validation_schema.Invalid as e:
55 errors = e.children
55 errors = e.children
56
56
57 def url_generator(**kw):
57 def url_generator(**kw):
58 q = urllib.quote(safe_str(search_query))
58 q = urllib.quote(safe_str(search_query))
59 return update_params(
59 return update_params(
60 "?q=%s&type=%s" % (q, safe_str(search_type)), **kw)
60 "?q=%s&type=%s" % (q, safe_str(search_type)), **kw)
61
61
62 c = tmpl_context
62 c = tmpl_context
63 search_query = search_params.get('search_query')
63 search_query = search_params.get('search_query')
64 search_type = search_params.get('search_type')
64 search_type = search_params.get('search_type')
65 search_sort = search_params.get('search_sort')
65 search_sort = search_params.get('search_sort')
66 if search_params.get('search_query'):
66 if search_params.get('search_query'):
67 page_limit = search_params['page_limit']
67 page_limit = search_params['page_limit']
68 requested_page = search_params['requested_page']
68 requested_page = search_params['requested_page']
69
69
70 try:
70 try:
71 search_result = searcher.search(
71 search_result = searcher.search(
72 search_query, search_type, c.auth_user, repo_name,
72 search_query, search_type, c.auth_user, repo_name,
73 requested_page, page_limit, search_sort)
73 requested_page, page_limit, search_sort)
74
74
75 formatted_results = Page(
75 formatted_results = Page(
76 search_result['results'], page=requested_page,
76 search_result['results'], page=requested_page,
77 item_count=search_result['count'],
77 item_count=search_result['count'],
78 items_per_page=page_limit, url=url_generator)
78 items_per_page=page_limit, url=url_generator)
79 finally:
79 finally:
80 searcher.cleanup()
80 searcher.cleanup()
81
81
82 if not search_result['error']:
82 if not search_result['error']:
83 execution_time = '%s results (%.3f seconds)' % (
83 execution_time = '%s results (%.3f seconds)' % (
84 search_result['count'],
84 search_result['count'],
85 search_result['runtime'])
85 search_result['runtime'])
86 elif not errors:
86 elif not errors:
87 node = schema['search_query']
87 node = schema['search_query']
88 errors = [
88 errors = [
89 validation_schema.Invalid(node, search_result['error'])]
89 validation_schema.Invalid(node, search_result['error'])]
90
90
91 c.perm_user = c.auth_user
91 c.perm_user = c.auth_user
92 c.repo_name = repo_name
92 c.repo_name = repo_name
93 c.sort = search_sort
93 c.sort = search_sort
94 c.url_generator = url_generator
94 c.url_generator = url_generator
95 c.errors = errors
95 c.errors = errors
96 c.formatted_results = formatted_results
96 c.formatted_results = formatted_results
97 c.runtime = execution_time
97 c.runtime = execution_time
98 c.cur_query = search_query
98 c.cur_query = search_query
99 c.search_type = search_type
99 c.search_type = search_type
100 c.searcher = searcher
100 c.searcher = searcher
101
101
102
102
103 class SearchView(BaseAppView):
103 class SearchView(BaseAppView):
104 def load_default_context(self):
104 def load_default_context(self):
105 c = self._get_local_tmpl_context()
105 c = self._get_local_tmpl_context()
106 self._register_global_c(c)
106 self._register_global_c(c)
107 return c
107 return c
108
108
109 @LoginRequired()
109 @LoginRequired()
110 @view_config(
110 @view_config(
111 route_name='search', request_method='GET',
111 route_name='search', request_method='GET',
112 renderer='rhodecode:templates/search/search.mako')
112 renderer='rhodecode:templates/search/search.mako')
113 def search(self):
113 def search(self):
114 c = self.load_default_context()
114 c = self.load_default_context()
115 search(self.request, c, repo_name=None)
115 search(self.request, c, repo_name=None)
116 return self._get_template_context(c)
116 return self._get_template_context(c)
117
117
118
118
119 class SearchRepoView(RepoAppView):
119 class SearchRepoView(RepoAppView):
120 def load_default_context(self):
120 def load_default_context(self):
121 c = self._get_local_tmpl_context()
121 c = self._get_local_tmpl_context()
122 self._register_global_c(c)
122 self._register_global_c(c)
123 return c
123 return c
124
124
125 @LoginRequired()
125 @LoginRequired()
126 @HasRepoPermissionAnyDecorator('repository.admin')
126 @HasRepoPermissionAnyDecorator(
127 'repository.read', 'repository.write', 'repository.admin')
127 @view_config(
128 @view_config(
128 route_name='search_repo', request_method='GET',
129 route_name='search_repo', request_method='GET',
129 renderer='rhodecode:templates/search/search.mako')
130 renderer='rhodecode:templates/search/search.mako')
130 def search_repo(self):
131 def search_repo(self):
131 c = self.load_default_context()
132 c = self.load_default_context()
132 search(self.request, c, repo_name=self.db_repo_name)
133 search(self.request, c, repo_name=self.db_repo_name)
133 return self._get_template_context(c)
134 return self._get_template_context(c)
General Comments 0
You need to be logged in to leave comments. Login now