diff --git a/farmdata2/farmdata2_modules/fd2_barn_kit/fd2_barn_kit.module b/farmdata2/farmdata2_modules/fd2_barn_kit/fd2_barn_kit.module index 634ca13b5f..4d49993f22 100644 --- a/farmdata2/farmdata2_modules/fd2_barn_kit/fd2_barn_kit.module +++ b/farmdata2/farmdata2_modules/fd2_barn_kit/fd2_barn_kit.module @@ -98,8 +98,21 @@ function fd2_barn_kit_preprocess_page() { // Define some useful variables in the JavaScript for page. // the @ symbol surpresses the warning when a user attempts to access a page without being logged in + global $user; - @$cmd="var fd2UserID=".$user->uid."; var fd2UserName='".$user->name."';"; + + $account = user_load($user->uid); + // Map from roleID to role name + $roleMap = user_roles(); + // Get the roles assigned to the user. + $roles = array(); + foreach ($account->roles as $role => $roleName) { + $roles[] = $roleMap[$role]; + } + + @$cmd="var fd2UserID=".$user->uid."; var fd2UserName='".$user->name."'; ;"; drupal_add_js($cmd,'inline'); + $roleCMD = "var fd2Role = " . json_encode($roles) . ";"; // JSON encode roles array + drupal_add_js($roleCMD, 'inline'); } }; diff --git a/farmdata2/farmdata2_modules/fd2_example/cypress/fd2vars.spec.js b/farmdata2/farmdata2_modules/fd2_example/cypress/fd2vars.spec.js index 562a9c161c..6aa6a5f3a6 100644 --- a/farmdata2/farmdata2_modules/fd2_example/cypress/fd2vars.spec.js +++ b/farmdata2/farmdata2_modules/fd2_example/cypress/fd2vars.spec.js @@ -19,7 +19,10 @@ describe('Check the JS vars defined by the FD2 Examples module', () => { cy.visit('/farm/fd2-barn-kit') cy.window().its('fd2UserID').should('equal',1); - cy.window().its('fd2UserName').should('equal','admin'); + cy.window().its('fd2UserName').should('equal', 'admin'); + cy.window().its('fd2Role').then((variable) => { + expect(variable).to.deep.equal(["authenticated user", "Farm Manager", "Farm Worker", "Farm Viewer"]) + }); }) it('Log in as manager1 and check tha vars exist.', () => { @@ -27,7 +30,10 @@ describe('Check the JS vars defined by the FD2 Examples module', () => { cy.visit('/farm/fd2-barn-kit') cy.window().its('fd2UserID').should('equal',3); - cy.window().its('fd2UserName').should('equal','manager1'); + cy.window().its('fd2UserName').should('equal', 'manager1'); + cy.window().its('fd2Role').then((variable) => { + expect(variable).to.deep.equal(["authenticated user", "Farm Manager", "Farm Worker", "Farm Viewer"]) + }); }) it('Log in as worker1 and check tha vars exist.', () => { @@ -35,7 +41,10 @@ describe('Check the JS vars defined by the FD2 Examples module', () => { cy.visit('/farm/fd2-barn-kit') cy.window().its('fd2UserID').should('equal',5); - cy.window().its('fd2UserName').should('equal','worker1'); + cy.window().its('fd2UserName').should('equal', 'worker1'); + cy.window().its('fd2Role').then((variable) => { + expect(variable).to.deep.equal(["authenticated user", "Farm Worker", "Farm Viewer"]) + }); }) // Currently guest users cannot see any FD2 tabs so no check for them. diff --git a/farmdata2/farmdata2_modules/fd2_example/fd2_example.module b/farmdata2/farmdata2_modules/fd2_example/fd2_example.module index 6b080cfce8..de2040bf10 100644 --- a/farmdata2/farmdata2_modules/fd2_example/fd2_example.module +++ b/farmdata2/farmdata2_modules/fd2_example/fd2_example.module @@ -133,7 +133,22 @@ function fd2_example_preprocess_page() { // Define some useful variables in the JavaScript for page. // the @ symbol surpresses the warning when a user attempts to access a page without being logged in global $user; - @$cmd="var fd2UserID=".$user->uid."; var fd2UserName='".$user->name."';"; + + + $account = user_load($user->uid); + // Map from roleID to role name + $roleMap = user_roles(); + // Get the roles assigned to the user. + $roles = array(); + foreach ($account->roles as $role => $roleName) { + $roles[] = $roleMap[$role]; + } + + @$cmd="var fd2UserID=".$user->uid."; var fd2UserName='".$user->name."'; ;"; drupal_add_js($cmd,'inline'); + $roleCMD = "var fd2Role = " . json_encode($roles) . ";"; // JSON encode roles array + drupal_add_js($roleCMD, 'inline'); + + } }; \ No newline at end of file diff --git a/farmdata2/farmdata2_modules/fd2_example/maps/maps.html b/farmdata2/farmdata2_modules/fd2_example/maps/maps.html index d145430598..7e6cf38523 100644 --- a/farmdata2/farmdata2_modules/fd2_example/maps/maps.html +++ b/farmdata2/farmdata2_modules/fd2_example/maps/maps.html @@ -1,37 +1,48 @@ -
The farmdata2_modules/FarmData2/fd2_tabs/resources/FarmOSAPI.js file provide a library of JavaScript convenience functions for accessing the FarmOS API. This page gives examples of how to use these functions for loading maps from farmOS id numbers to textual names and vice versa (E.g. from user id to user name or crop id to crop name). The js library is include in the page by the fd2_example.info file.
+The farmdata2_modules/FarmData2/fd2_tabs/resources/FarmOSAPI.js file provide a library of JavaScript convenience + functions for accessing the FarmOS API. This page gives examples of how to use these functions for loading maps + from farmOS id numbers to textual names and vice versa (E.g. from user id to user name or crop id to crop name). + The js library is include in the page by the fd2_example.info file.
+ +This page specifcially demonstrates the maps for going from user id to username and vice versa. The use of other + maps is similar. The documentation for the maps can be viewed by opening + farmdata2_modules/fd2_tabs/resources/doc/index.html from the repo in your browser. You can find the full source + for the library in farmdata2_modules/FarmData2/fd2_tabs/resources/FarmOSAPI.js.
-This page specifcially demonstrates the maps for going from user id to username and vice versa. The use of other maps is similar. The documentation for the maps can be viewed by opening farmdata2_modules/fd2_tabs/resources/doc/index.html from the repo in your browser. You can find the full source for the library in farmdata2_modules/FarmData2/fd2_tabs/resources/FarmOSAPI.js.
-Examples of how to use Cypress to test the elements on this page can be found in the maps.spec.js file.
The userToIDMap map can be used to go from a username to a user id:
The IDToUserMap map can be used to go from a user id to a username:
The full list of users known to the sustem is:
| User ID | User Name |
|---|---|
| User ID | +User Name | +
| {{ id }} | {{ idToUserMap.get(id) }} | {{ id }} | +{{ idToUserMap.get(id) }} | +
FarmData2 defines a few useful variables in every page. This page demonstrates how those variables can be brought into the Vue instance and used in scripts.
+FarmData2 defines a few useful variables in every page. This page demonstrates how those variables can be brought + into the Vue instance and used in scripts.
Examples of how to use Cypress to test the elements on this page can be found in the vars.spec.js file.
@@ -10,6 +11,7 @@The current values of these variables are:
@@ -19,10 +21,11 @@The current values of the configuration variables are:
| Configuration Variables | @@ -53,41 +56,41 @@ - \ No newline at end of file + //Allows Vue to be accessed in Dev Tools + Vue.config.devtools = true; + \ No newline at end of file
|---|