Problem Solving Studio
File Permissions tutorial
Contents
Set-up
This tutorial assumes that you just completed the following warm-up exercise:
https://docs.google.com/document/d/1mOCifxWngSUHSFXNLhk0ifq_6aL13gaRsT2mZUUvlig/export?format=pdf
(So you should start with that if you haven't already done it, or if you need a refresher)
You will be working in view.js; most of your code can go where it says "Define your dialogs and panels here" at the top. You will also need to look at view_helper.js, controller.js, and model.js for some of the functions you'll be using.
- References
- The File Permissions code uses the jquery and jquery-ui libraries. You may find it useful to refer to the following documentation and/or tutorials (though you don't need to read through them right this moment):
- jquery:
- jquery-ui:
- The file permissions code also uses two icon libraries (oops!), so you can use icons from either one of these:
Effective Permissions Panel
Step 1: make the panel
First, we will make an Effective Permissions panel using the defineNewEffectivePermissions function in tracker.ts . Read the function definition and the comment above it.
Call this function in view.js , and store the result in a variable .
- You should choose a meaningful id_prefix : think of it the same way you think about choosing a good variable name.
The ids of all the elements inside this new panel will start with the id_prefix you choose. And the ids are what will get logged when users click those elements.
- And yes, you do want to add the info column for this exercise.
- Nothing should happen yet. (we still have to put the panel onto the page)
Step 2: show the panel
defineNewEffectivePermissions creates the effective permissions panel but doesn't put it onto the page.
append the result to the element with id sidepanel
- to select an element with a given id, use # :
$('#element_id')
- then use the append function the same way we did in the warm-up.
- Tip: Throughout this tutorial, when part of a code snippet is in non-bold italics, e.g.element_id
- It represents a placeholder which should be replaced with something appropriate, e.g. the id of the element you are trying to select
If you reload the file permissions page(any page for any of the scenarios), you should now see an effective permissions table show up in the side panel, next to the files! It is not displaying any information, because we haven't specified which file and user to display info for.
In the dev console, try setting the filepath and username attributes of your new panel:
$('#my_id_prefix').attr('filepath', '/C')
- The attr function in jquery lets you set or get an HTML attribute of the elements you selected. So after you execute the code above, the affected element will look something like this:
<div id="my_id_prefix" filepath="/C">
- attr with two parameters sets the attribute; attr with one parameter gets the attribute.
- See the documentation for details: https://api.jquery.com/attr/
Nothing should happen yet. (both filepath and username have to be set before something happens)
Now do the same to set the 'username' attribute to 'administrator'. The effective permissions panel should get populated with all checkboxes! (the administrator has full permissions on /C for all the scenarios).
Inspect the element of the effective permissions panel. it should now have the attributes you just set, and should look something like:
<div ... filepath="/C" username="administrator">
- Tip: You can also edit the attributes directly in the Elements view of the dev console - just double-click on the attribute to edit its value.
- This should have the same exact effect as calling .attr in the console.
Step 2.5: Try a different filepath
In order to be able to test more interesting things, pick a more relevant filepath than "/C".
Choose one of the files that are relevant to the scenario you are testing this on: for example, if this is one of the scenarios about restricting the permissions for important_file.txt, you can use the filepath for important_file.txt.
To quickly get the correct path for a given file, you can Inspect the permissions button for that file again, and look for the path attribute on that button. For example, the <button> element for important_file.txt looks something like this:
<button ... path="/C/presentation_documents/important_file.txt" ...>
Step 3: add a user selector
We want to add an interface element to allow us to select a user.
Use the defineNewUserSelectField function in tracker.ts to create such an element. for the third parameter, just use a blank function like so:
function(selected_user){}
This function will be called whenever a new user is selected, and the parameter selectedUser will contain the name of the newly selected user. (We'll put something useful inside it in the next step)
append the user select field you just created to the same sidepanel element.
- Tip: The order you append things in is the order they will appear in. The order you append things doesn't have to be the same as the order you define them. I like to define and configure a group of objects first, and then append them all. You can also try prepend to add things to the beginning instead of the end.
Try it out. There should be a (blank) field and a button. when you click on the button, a user selection dialog appears. When you select a user and click OK, the user appears in the field!
But NOTHING else should happen yet. (we have to manually connect the user selector with the effective permissions panel)
Step 4: Connect the user selector and the effective permissions
Whenever a user gets selected, we want to change the username attribute of our effective permissions panel to whoever was selected.
The code to do that goes inside that blank function(selectedUser: any){} you just made earlier. (Note: you can use as many newlines as you want inside the {})
Use the same exact line of code that you used before to set the user to 'administrator', but set it to the selectedUser parameter instead of 'administrator'.
For the purpose of this tutorial, just hardcode the file path attribute. Use the same line of code as you used in the console to set the filepath - just put it somewhere in your code in view.js, so that the filepath gets set when the page loads.
That should do it! Try selecting users to see their permissions for the file path you hard-coded in.
Recap
- We created an effective permissions panel.
- It listens for when its own filepath and username attributes change, and automatically loads the effective permissions for the given file and user.
- We then created a user-selector element.
- It gave us a UI for selecting a user.
- We connected the user selector to the effective permissions.
- We did this by changing the username attribute of the effective permissions panel each time a user was selected in the user selector.
- Finally, we hardcoded a filepath attribute for the effective permissions panel.
- Now the effective permissions panel displays the permissions for that hard-coded file, for any user we choose through the user selector.
Permissions Explanation
Now let's make those 'i' icons do something! Specifically, we will make each one open a dialog with an explanation of why the effective permission for a given file, user, and permission type is set that way.
Step 1: Define the dialog
We will define a single dialog for the explanations, and open/close it programmatically, populating it with the right info. I strongly recommend following this pattern for this application: define a single dialog for each type of thing you want to show, instead of creating and destroying them on the fly.
Use the function define_new_dialog in view_helper.js to define a new (blank) dialog. This will return a jquery-ui dialog object.
- Tip: The third parameter of define_new_dialog just gets passed along to the jquery-ui dialog as the set of options to use in the dialog. It can be used to set things such as height/width of the dialog, buttons that are part of the dialog, etc.
- If you don't want to set any extra options, you can just omit that parameter.
- See https://jqueryui.com/dialog/ for examples and documentation
Nothing should happen yet. (the dialog is invisible until we open it)
Step 2: Open the dialog on click
Unlike a standard jquery-ui dialog, this one is configured to start out closed by default: we usually only want to open up dialogs when the user clicks on something.
In this case, we want to open the dialog each time one of the 'info' icons is clicked:
Inspect one of these icons to see what classes they are using.
- The classes that start with fa make that element magically turn into an Font Awesome icon.
- The other class is the one we will use to select all of these specific icons.
Using a class selector, select all the 'info' icons and attach a click listener to them:
$('.my_class').click(function(){
// stuff that should happen on click goes here
})
First, let's test that this on-click function works as expected. Add a line of code inside of that function to log a simple message, e.g.:
console.log('clicked!')
Check that it's working: does it say 'clicked!' in the console whenever you click any of the "info" icons?
Now that we know the click listener is working, add another line of code inside of it to open our blank dialog. (see comment for define_new_dialog function for how to open jquery-ui dialogs).
Test it out again: does a blank dialog show up each time you click one of the info icons?
Step 3: Log useful information
Each time this dialog is opened, we'll want to put information about the current user, filename, and permission into it.
First, try logging these three things to the dev console using console.log() inside the on-click function:
- We can get the current username and filepath from the effective permissions element, like so:
$('#my_id_prefix').attr('filepath')
$('#my_id_prefix').attr('username')
- (recall that attr with two parameters sets the attribute; attr with one parameter gets the attribute.)
- The relevant permission type is conveniently stored in the attribute permission_name of the 'info' icon we just clicked!
- Inside the click function, $( this ) will get us the icon we just clicked.
is it working? reload the page and click on some of the 'info' icons. Look at the dev console: is it outputting what you expect?
Step 4: Get and display explanation
Now you have the username, filepath, and permission type inside the click function.
- Use the function allow_user_action from model.js to get whether the action is allowed, and the explanation for why.
- There is a bit of a gotcha here: what you have is a filepath and a username; what you need for this function is a file object and a user object.
- Use the object path_to_file (defined in controller.js ) to get the file object for a given path, e.g.:
my_file_obj_var = path_to_file[my_filename_var]
- Likewise, use the object all_users to get the user object for a given username.
- Use the get_explanation_text function (in view_helper.js) to go from explanation object to a string of (mostly) human-readable text.
- Add the explanation text to the dialog you just opened.
- You can use append() as before, though you might then have to empty() the dialog each time (otherwise the explanations just keep stacking…).
- Or you can try text() or html() to replace the contents each time.
- See jquery documentation for details and examples:
That should do it! Some interesting types of permissions to look at the explanation for:
- A "take ownership" permission for an administrator
- Any permission for anyone that's not an administrator on /C
- An inherited permission (this will only exist for some tasks)
- A permission set for a group, looking through the effective permissions of a member of that group (this will only exist for a couple of the tasks)
Recap
- We defined a new empty dialog
- We added a click listener so that this dialog would open whenever any of the "info" icons are clicked
- We gathered the relevant information about what was clicked - the file, user, and permission name - and generated an explanation for that file/user/permission
- We put that explanation in the dialog, overwriting anything that was previously there.
Challenges / bonus ideas
- Try changing get_explanation_text, or making your own function, to make the explanation text more human-readable
- Try setting the dialog options so that it has an "OK" button, and the dialog closes when you click that button (see https://jqueryui.com/dialog/ )
- Remember how you added text to the permissions button in the warm-up? Add that code to your view.js to make it permanent.
- Add a second button next to the permissions button for each file; make it so that when that button is clicked, the filepath attribute for our effective permissions panel is changed to that file.
- What happens when you click that button on a folder? Why doesn't that happen with the permissions button?
- it's because I fixed it. Look for the $('.permbutton').click definition inside view.js.
- make sure you copy that emitter line too!
- it's because I fixed it. Look for the $('.permbutton').click definition inside view.js.
- What happens when you click that button on a folder? Why doesn't that happen with the permissions button?
- Look through the other functions in view_helper.js. There are define_* helpers for all or most of the major components for the baseline UI. Think about how you might use them in your UI. Is there something else you'd like to have, or something you're not sure how to do? tell me or post on Piazza.
Published by Google Drive–Report Abuse–Updated automatically every 5 minutes