From 9316a590439b25e113eed2418045892eb821dfcd Mon Sep 17 00:00:00 2001 From: nguyenbanhducA1K51 Date: Sun, 3 Dec 2023 02:54:11 -0500 Subject: [PATCH 1/3] add user role/permission to module fd2_example --- .../fd2_example/fd2_example.module | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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 From a8a69e26e7de911f129a8baa662e53bfe9b09748 Mon Sep 17 00:00:00 2001 From: nguyenbanhducA1K51 Date: Mon, 11 Dec 2023 02:28:18 -0500 Subject: [PATCH 2/3] add illustration --- .../fd2_example/maps/maps.html | 118 ++++++++++-------- 1 file changed, 65 insertions(+), 53 deletions(-) diff --git a/farmdata2/farmdata2_modules/fd2_example/maps/maps.html b/farmdata2/farmdata2_modules/fd2_example/maps/maps.html index d145430598..a1e99a2b75 100644 --- a/farmdata2/farmdata2_modules/fd2_example/maps/maps.html +++ b/farmdata2/farmdata2_modules/fd2_example/maps/maps.html @@ -1,37 +1,49 @@ -
+
-

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 logged in user is {{ userName }}.
  • -
  • That user's ID in the map is: +
  • That user's ID in the map is: {{ userToIDMap.get(userName) }} +
  • The user's roles could be get using fd2Role property defined in tab module : {{userRole}}

The IDToUserMap map can be used to go from a user id to a username:

  • The logged in user id is {{ userID }}.
  • -
  • That user's name in the map is: +
  • That user's name in the map is: - {{ idToUserMap.get(userID.toString()) }} + {{ idToUserMap.get(userID.toString()) }}

The full list of users known to the sustem is:

    - + + + + - + + +
    User IDUser Name
    User IDUser Name
    {{ id }}{{ idToUserMap.get(id) }}
    {{ id }}{{ idToUserMap.get(id) }}
@@ -50,51 +62,51 @@
- \ No newline at end of file + //Allows Vue to be accessed in Dev Tools + Vue.config.devtools = true; + \ No newline at end of file From 9626167d680c1614c32e6e563bffdba2d6ab574d Mon Sep 17 00:00:00 2001 From: nguyenbanhducA1K51 Date: Thu, 14 Dec 2023 18:29:57 -0500 Subject: [PATCH 3/3] add variable test and move illustration to var subtab --- .../fd2_barn_kit/fd2_barn_kit.module | 15 +++- .../fd2_example/cypress/fd2vars.spec.js | 15 +++- .../fd2_example/maps/maps.html | 3 - .../fd2_example/vars/vars.html | 79 ++++++++++--------- 4 files changed, 67 insertions(+), 45 deletions(-) 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/maps/maps.html b/farmdata2/farmdata2_modules/fd2_example/maps/maps.html index a1e99a2b75..7e6cf38523 100644 --- a/farmdata2/farmdata2_modules/fd2_example/maps/maps.html +++ b/farmdata2/farmdata2_modules/fd2_example/maps/maps.html @@ -19,7 +19,6 @@
  • The logged in user is {{ userName }}.
  • That user's ID in the map is: {{ userToIDMap.get(userName) }} -
  • The user's roles could be get using fd2Role property defined in tab module : {{userRole}}
  • The IDToUserMap map can be used to go from a user id to a username:

    @@ -69,8 +68,6 @@ // Provide easy access to the FarmDat2 variables in vue. userID: fd2UserID, userName: fd2UserName, - userRole: fd2Role, - // Vue variables to hold the maps that are loaded from farmOS // Note: Making a new map here prevents null reference exceptions if the // map has not been retrieved yet when the page is rendered. diff --git a/farmdata2/farmdata2_modules/fd2_example/vars/vars.html b/farmdata2/farmdata2_modules/fd2_example/vars/vars.html index 6f4cee5d22..53974ba05a 100644 --- a/farmdata2/farmdata2_modules/fd2_example/vars/vars.html +++ b/farmdata2/farmdata2_modules/fd2_example/vars/vars.html @@ -1,8 +1,9 @@ -
    +
    -

    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 @@
    • UserID: Contains the id number of the logged in user.
    • UserName: Contains the text username of the logged in user.
    • +
    • UserRole: Contains roles of logged in user.

    The current values of these variables are:

    @@ -19,10 +21,11 @@
  • UserID: {{ userID }}
  • UserName: {{ userName }}
  • +
  • UserRole: {{userRole}}
  • The current values of the configuration variables are:

      - +
      @@ -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
      Configuration Variables