Ver Fonte

HUE-688 [beeswax] Beeswax shouldn't show saved queries from other users

Share saved queries with all users. If 'share_saved_queries' is set
to false, saved queries are visible only to the owner and administrators.
Romain Rigaux há 13 anos atrás
pai
commit
5b8476c3d4

+ 7 - 1
apps/beeswax/src/beeswax/conf.py

@@ -15,7 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 """Configuration options for the Hive UI (Beeswax)."""
-from desktop.lib.conf import Config
+from desktop.lib.conf import Config, coerce_bool
 import os.path
 
 BEESWAX_SERVER_HOST = Config(
@@ -96,3 +96,9 @@ BROWSE_PARTITIONED_TABLE_LIMIT = Config(
   default=250,
   type=int,
   help='Set a LIMIT clause when browsing a partitioned table. A positive value will be set as the LIMIT. If 0 or negative, do not set any limit.')
+
+SHARE_SAVED_QUERIES = Config(
+  key='share_saved_queries',
+  default=True,
+  type=coerce_bool,
+  help='Share saved queries with all users. If set to false, saved queries are visible only to the owner and administrators.')

+ 44 - 0
apps/beeswax/src/beeswax/tests.py

@@ -41,6 +41,7 @@ import beeswax.hive_site
 import beeswax.models
 import beeswax.report
 import beeswax.views
+from beeswax import conf
 from beeswax.views import parse_results, collapse_whitespace
 from beeswax.test_base import make_query, wait_for_query_to_finish, verify_history
 from beeswax.test_base import BeeswaxSampleProvider
@@ -492,6 +493,49 @@ for x in sys.stdin:
     resp = do_view('sort=-type&user=test&type=hql&text=Rubbish')
     assert_true('rubbish' in resp.content)
 
+    # Test personal saved queries permissions
+    _make_query(self.client, "select one", name='client 1 query', submission_type='Save')
+    _make_query(self.client, "select two", name='client 2 query', submission_type='Save')
+
+    conf.SHOW_ONLY_PERSONAL_SAVED_QUERIES.set_for_testing(True)
+    resp = self.client.get('/beeswax/list_designs')
+    assert_true('select one' in resp.content)
+    assert_true('select two' in resp.content)
+
+    conf.SHOW_ONLY_PERSONAL_SAVED_QUERIES.set_for_testing(False)
+    resp = self.client.get('/beeswax/list_designs')
+    assert_true('select one' in resp.content)
+    assert_true('select two' in resp.content)
+
+    # Login as someone else
+    client2 = make_logged_in_client('not_me')
+    conf.SHOW_ONLY_PERSONAL_SAVED_QUERIES.set_for_testing(True)
+    resp = client2.get('/beeswax/list_designs')
+    assert_true('select one' not in resp.content)
+    assert_true('select two' not in resp.content)
+
+    conf.SHOW_ONLY_PERSONAL_SAVED_QUERIES.set_for_testing(False)
+    resp = client2.get('/beeswax/list_designs')
+    assert_true('select one' in resp.content)
+    assert_true('select two' in resp.content)
+    client2.logout()
+
+    # Login as super user
+    client3 = make_logged_in_client('admin', is_superuser=True)
+    conf.SHOW_ONLY_PERSONAL_SAVED_QUERIES.set_for_testing(True)
+    resp = client3.get('/beeswax/list_designs')
+    assert_true('select one' in resp.content)
+    assert_true('select two' in resp.content)
+
+    conf.SHOW_ONLY_PERSONAL_SAVED_QUERIES.set_for_testing(False)
+    resp = client3.get('/beeswax/list_designs')
+    assert_true('select one' in resp.content)
+    assert_true('select two' in resp.content)
+    client3.logout()
+
+    # Cleaning
+    beeswax.models.SavedQuery.objects.all()[:2].delete()
+
 
   def test_my_queries(self):
     """Test the "My Queries" page"""

+ 16 - 5
apps/beeswax/src/beeswax/views.py

@@ -501,7 +501,7 @@ def save_design(request, form, type, design, explicit_save):
 
 def list_designs(request):
   """
-  View function for show all saved queries
+  View function for show all saved queries.
 
   We get here from /beeswax/list_designs?filterargs, with the options being:
     page=<n>    - Controls pagination. Defaults to 1.
@@ -512,10 +512,18 @@ def list_designs(request):
                   Accepts the form "-date", which sort in descending order.
                   Default to "-date".
     text=<frag> - Search for fragment "frag" in names and descriptions.
+
+  Depending on Beeswax configuration parameter ``SHOW_ONLY_PERSONAL_SAVED_QUERIES``,
+  only the personal queries of the user will be returned (even if another user is
+  specified in ``filterargs``).
   """
   DEFAULT_PAGE_SIZE = 10
 
-  page, filter_params = _list_designs(request.GET, DEFAULT_PAGE_SIZE)
+  if conf.SHARE_SAVED_QUERIES.get() or request.user.is_superuser:
+    user = None
+  else:
+    user = request.user
+  page, filter_params = _list_designs(request.GET, DEFAULT_PAGE_SIZE, user=user)
   return render('list_designs.mako', request, {
     'page': page,
     'filter_params': filter_params,
@@ -523,12 +531,14 @@ def list_designs(request):
   })
 
 
-def _list_designs(querydict, page_size, prefix=""):
+def _list_designs(querydict, page_size, prefix="", user=None):
   """
-  _list_designs(querydict, page_size, prefix) -> (page, filter_param)
+  _list_designs(querydict, page_size, prefix, user) -> (page, filter_param)
 
   A helper to gather the designs page. It understands all the GET params in
   ``list_designs``, by reading keys from the ``querydict`` with the given ``prefix``.
+  If a ``user`` is specified, only the saved queries of this user will be returned.
+  This has priority over the ``user`` in the ``querydict`` parameter.
   """
   DEFAULT_SORT = ('-', 'date')                  # Descending date
 
@@ -542,7 +552,8 @@ def _list_designs(querydict, page_size, prefix=""):
 
   # Filtering. Only display designs explicitly saved.
   db_queryset = models.SavedQuery.objects.filter(is_auto=False)
-  user = querydict.get(prefix + 'user')
+  if user is None:
+    user = querydict.get(prefix + 'user')
   if user is not None:
     db_queryset = db_queryset.filter(owner__username=user)
 

+ 4 - 0
desktop/conf/pseudo-distributed.ini.tmpl

@@ -276,6 +276,10 @@
   # may override this setting.
   ## beeswax_server_heapsize=1000
 
+  # Share saved queries with all users. If set to false, saved queries are
+  # visible only to the owner and administrators.
+  ## share_saved_queries=true
+
 
 ###########################################################################
 # Settings to configure Job Designer.