##// END OF EJS Templates
api: expose audit-logs API
ergo -
r4353:7a7df1ad default
parent child Browse files
Show More
@@ -1,118 +1,152 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21
22 22 import logging
23 23
24 24 from rhodecode.api import jsonrpc_method
25 from rhodecode.api.exc import JSONRPCValidationError
26 from rhodecode.api.utils import Optional
25 from rhodecode.api.exc import JSONRPCValidationError, JSONRPCForbidden
26 from rhodecode.api.utils import Optional, has_superadmin_permission
27 27 from rhodecode.lib.index import searcher_from_config
28 from rhodecode.lib.user_log_filter import user_log_filter
28 29 from rhodecode.model import validation_schema
30 from rhodecode.model.db import joinedload, UserLog
29 31 from rhodecode.model.validation_schema.schemas import search_schema
30 32
31 33 log = logging.getLogger(__name__)
32 34
33 35
34 36 @jsonrpc_method()
35 37 def search(request, apiuser, search_query, search_type, page_limit=Optional(10),
36 38 page=Optional(1), search_sort=Optional('desc:date'),
37 39 repo_name=Optional(None), repo_group_name=Optional(None)):
38 40 """
39 41 Fetch Full Text Search results using API.
40 42
41 43 :param apiuser: This is filled automatically from the |authtoken|.
42 44 :type apiuser: AuthUser
43 45 :param search_query: Search query.
44 46 :type search_query: str
45 47 :param search_type: Search type. The following are valid options:
46 48 * commit
47 49 * content
48 50 * path
49 51 :type search_type: str
50 52 :param page_limit: Page item limit, from 1 to 500. Default 10 items.
51 53 :type page_limit: Optional(int)
52 54 :param page: Page number. Default first page.
53 55 :type page: Optional(int)
54 56 :param search_sort: Search sort order.Must start with asc: or desc: Default desc:date.
55 57 The following are valid options:
56 58 * asc|desc:message.raw
57 59 * asc|desc:date
58 60 * asc|desc:author.email.raw
59 61 * asc|desc:message.raw
60 62 * newfirst (old legacy equal to desc:date)
61 63 * oldfirst (old legacy equal to asc:date)
62 64
63 65 :type search_sort: Optional(str)
64 66 :param repo_name: Filter by one repo. Default is all.
65 67 :type repo_name: Optional(str)
66 68 :param repo_group_name: Filter by one repo group. Default is all.
67 69 :type repo_group_name: Optional(str)
68 70 """
69 71
70 72 data = {'execution_time': ''}
71 73 repo_name = Optional.extract(repo_name)
72 74 repo_group_name = Optional.extract(repo_group_name)
73 75
74 76 schema = search_schema.SearchParamsSchema()
75 77
76 78 try:
77 79 search_params = schema.deserialize(
78 80 dict(search_query=search_query,
79 81 search_type=search_type,
80 82 search_sort=Optional.extract(search_sort),
81 83 page_limit=Optional.extract(page_limit),
82 84 requested_page=Optional.extract(page))
83 85 )
84 86 except validation_schema.Invalid as err:
85 87 raise JSONRPCValidationError(colander_exc=err)
86 88
87 89 search_query = search_params.get('search_query')
88 90 search_type = search_params.get('search_type')
89 91 search_sort = search_params.get('search_sort')
90 92
91 93 if search_params.get('search_query'):
92 94 page_limit = search_params['page_limit']
93 95 requested_page = search_params['requested_page']
94 96
95 97 searcher = searcher_from_config(request.registry.settings)
96 98
97 99 try:
98 100 search_result = searcher.search(
99 101 search_query, search_type, apiuser, repo_name, repo_group_name,
100 102 requested_page=requested_page, page_limit=page_limit, sort=search_sort)
101 103
102 104 data.update(dict(
103 105 results=list(search_result['results']), page=requested_page,
104 106 item_count=search_result['count'],
105 107 items_per_page=page_limit))
106 108 finally:
107 109 searcher.cleanup()
108 110
109 111 if not search_result['error']:
110 112 data['execution_time'] = '%s results (%.4f seconds)' % (
111 113 search_result['count'],
112 114 search_result['runtime'])
113 115 else:
114 116 node = schema['search_query']
115 117 raise JSONRPCValidationError(
116 118 colander_exc=validation_schema.Invalid(node, search_result['error']))
117 119
118 120 return data
121
122
123 @jsonrpc_method()
124 def get_audit_logs(request, apiuser, query):
125 """
126 return full audit logs based on the query.
127
128 Please see `example query in admin > settings > audit logs` for examples
129
130 :param apiuser: This is filled automatically from the |authtoken|.
131 :type apiuser: AuthUser
132 :param query: filter query, example: action:repo.artifact.add date:[20200401 TO 20200601]"
133 :type query: str
134 """
135
136 if not has_superadmin_permission(apiuser):
137 raise JSONRPCForbidden()
138
139 filter_term = query
140 ret = []
141
142 # show all user actions
143 user_log = UserLog.query() \
144 .options(joinedload(UserLog.user)) \
145 .options(joinedload(UserLog.repository)) \
146 .order_by(UserLog.action_date.desc())
147
148 audit_log = user_log_filter(user_log, filter_term)
149
150 for entry in audit_log:
151 ret.append(entry)
152 return ret
General Comments 0
You need to be logged in to leave comments. Login now