Saturday, June 27, 2015

Triggering Python Script on Dashboard Open ( Example: Tab Level Security)

 
Prerequisites:-
 - Property Controls/Document Property
 - Python Script
 - Statistical Server in your project (TERR)
 - Personalized Information link

In Spotfire, we can apply Data level security using Personalized information links i.e., you can set up a data source to return only applicable information for a certain user or group. 
Example:- To return Asia Region sales data for Asia User group.

So restricting the data is possible to do but how about Tab Level Security at Dashboard?

Yes, its possible to implement Tab Level security using Python script. Build a Python script which should read the Current User/Current group based upon your requirement and implement the logic's accordingly. Lets take a simple example that the Dashboard has 2 tabs; Summary Tab & Details Tab and would like to restrict the Details Tab for a specific User group "Restricted" (Spotfire Group).

Assuming we have created the Python script and now the next step here would be how to trigger the Python Script on Opening of Dashboard? 

Below are the Detail steps to achieve our goal:

Step 1: Create a Data Table based on personalized Information link which should fetch the data whether the Current User belongs to Restricted group or not. The Query should like below:-

SELECT
   U1."USER_NAME" AS "USERNAME",
   G2."GROUP_NAME" AS "GROUPNAME"
FROM
   "SPOT_USR"."USERS" U1,
   "SPOT_USR"."GROUPS" G2,
   "SPOT_USR"."GROUP_MEMBERS" G3
WHERE
   (G3."MEMBER_USER_ID" = U1."USER_ID")
   AND (G2."GROUP_ID" = G3."GROUP_ID")
   AND (U1."USER_NAME" in (%CURRENT_USER%))
   AND (G2."GROUP_NAME" = 'Restricted')
   AND <conditions>


Step 2:  Python Script for deleting the page "Detail" is as below

for Page in Document.Pages:
    if Page.Title == "Detail":
        Document.Pages.Remove(Page) 


 Step 3: Now either update the python script to read Usergroup from DataTable which is based on Personalized Information or Assign the Usergroup value to a Document Property thru R-script

 a) Python Script to Read User Group from Data Table.

table=Document.Data.Tables["PersonalizedInformationLinkTable"]
minCol=table.Columns['GroupName']
minCursor=DataValueCursor.Create(minCol)

for row in table.GetRows(minCursor):
  Document.Properties["UserGroup"]= minCursor.CurrentValue;



b) Assign the Usergroup value to a Document Property thru R-script.

Register a new Data Function(TERR) with the script as below and define Input and Output Parameters  as Value

    DocProperty <- Input


 







Click on Run button and choose the Input as expression with definition as 
Max([PersonalizedInformationLinkTable].[GROUP_NAME])


 
Similarly, choose Output as Document Property and choose the property accordingly.





Step 3: Final Python script would look like

import Spotfire.Dxp
from Spotfire.Dxp.Data import *


#-------------Below lines if we fetch usergroup thru Python -----------------


table=Document.Data.Tables["PersonalizedInformationLinkTable"]
minCol=table.Columns['GroupName']
minCursor=DataValueCursor.Create(minCol)
for row in table.GetRows(minCursor):
    Document.Properties["UserGroup"]= minCursor.CurrentValue;
 

#--------------------------------------------------------------------------

 Document.Pages:
    if Document.Properties["UserGroup"]=="Restricted":
        if Page.Title == "Detail":       
        Document.Pages.Remove(Page)



Step 4: Create a Document Property called "DateTimeStamp" and assign the above the script which we have created.

Step 5: Create a simple Information link with one element that should fetch current DateTimeStamp

SELECT
   SYSTIMESTAMP AS "SYSTEMTIME"
FROM
   "SYS"."DUAL" D1
WHERE
   <conditions>


Step 6: Import this table to your Dashboard and assign the  SystemTime Element to the Document Property "DateTimeStamp" using R script (similar to steps 3b)

Step 7: Save the Dashboard

Now, as you Open or Refresh the Dashboard each time, the Time Stamp will be different & the document property will get updated and hence the Python script will be triggered.


PS: We can Trigger Python script on Opening of Dashboard using JScript too.




 

9 comments:

  1. Wouldn't the user be able to get the deleted tab back by clicking Undo when the analysis opens?

    ReplyDelete
  2. Thanks for the post.. I have found it very useful. I implemented this and applied a different twist to it, where I modified the script that restricts the page to get the Identity of the user and check that user against either the contents of a data table or string list, and restrict pages based on that.

    eg> import Spotfire.Dxp
    from Spotfire.Dxp.Data import *
    from System import Environment, Threading

    username = Threading.Thread.CurrentPrincipal.Identity.Name

    Document.Properties["username"] = username

    Document
    if Document.Properties["username"]!= "XXX": #insert list here or read a table
    for Page in Document.Pages:
    if Page.Title != "Summary":
    Document.Pages.Remove(Page)

    ReplyDelete
  3. That reminds me: Here's what I came up with to get around the security flaw that the user could click Undo after opening the file, which would undo the running of the script and restore the deleted pages (at least it does if you trigger the script using javascript). I included the same javascript trigger on each of the pages being deleted. Then even if the user undoes the deletion upon opening the file, every time they navigate to a forbidden page, it triggers the script which deletes the page. If they undo that, it takes them back to the starting page, so they never get to see the forbidden page(s).

    ReplyDelete

  4. Well Done ! the blog is great and Interactive it is Triggering Python Script on Dashboard Open ( Example: Tab Level Security) it is useful for students and Python Developers for more updates on python follow the link

    Python Online Training

    For more info on other technologies go with below links

    tableau online Training

    ServiceNow Online Training

    mulesoft Online Training

    ReplyDelete
  5. Really Thanks For Posting Such a Useful Content. Really Thanks For Sharing Such an Informative Post.
    Learn Mulesoft Online

    ReplyDelete
  6. Hello,
    Nice article… very useful
    thanks for sharing the information.
    servicenow cmdb training

    ReplyDelete
  7. Nice article, interesting to read…
    Thanks for sharing the useful information
    android development certification

    ReplyDelete