Skip to content

Hide view and edit tab for account team#672

Merged
tarunnjoshi merged 1 commit intodevelopfrom
hide-view-and-edit-tab-for-account-team
Dec 21, 2024
Merged

Hide view and edit tab for account team#672
tarunnjoshi merged 1 commit intodevelopfrom
hide-view-and-edit-tab-for-account-team

Conversation

@tarunnjoshi
Copy link
Copy Markdown
Member

@tarunnjoshi tarunnjoshi commented Dec 21, 2024

Hide view and edit tab for account team

Summary by CodeRabbit

  • New Features

    • Introduced role-based access control for tab visibility across various services, restricting 'view' and 'edit' tabs for non-admin users with specific roles.
    • Enhanced email notifications for material dispatch, including dynamic content based on dispatch details.
  • Bug Fixes

    • Improved error handling in methods related to user permissions and data validation.
  • Refactor

    • Updated methods to enhance logic related to user permissions and coordinator assignments without altering their signatures.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Dec 21, 2024

Walkthrough

The pull request introduces comprehensive modifications across multiple service classes within the Goonj CiviCRM extension. The primary focus is on implementing role-based access control for tab visibility, enhancing user permission management, and improving various service methods related to collection camps, dropping centers, and activities. The changes consistently apply similar patterns of role-based checks and method refinements across different service classes, suggesting a systematic approach to access control and functionality enhancement.

Changes

File Change Summary
wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php - Added role-based tab visibility control
- Updated camp status update logic
- Enhanced office details assignment
- Improved email notification mechanism
wp-content/civi-extensions/goonjcustom/Civi/DroppingCenterService.php - Implemented role-based tab visibility check
wp-content/civi-extensions/goonjcustom/Civi/GoonjActivitiesService.php - Added role-based tab permission checks
wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php - Added role-based tab visibility control
- Updated coordinator assignment logic
wp-content/civi-extensions/goonjcustom/Civi/InstitutionDroppingCenterService.php - Implemented role-based tab visibility
- Enhanced email notification method
- Updated MMT email HTML generation
wp-content/civi-extensions/goonjcustom/Civi/InstitutionGoonjActivitiesService.php - Added role-based tab permission checks
- Improved contact group addition method

Possibly related PRs

Suggested Labels

status : ready for review

Suggested Reviewers

  • nishant22029

Poem

🚦 Permissions dance, tabs take flight
Roles define who sees what's right
Code refactored with careful might
Access controlled, secure and tight
A symphony of digital delight! 🔒


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🔭 Outside diff range comments (2)
wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php (2)

Line range hint 472-476: Security concern: Sensitive information in URLs

The email URLs contain sensitive information like email addresses and contact numbers in query parameters. This could pose a security risk as:

  1. URLs might be logged in server logs
  2. URLs might be visible in browser history
  3. URLs might be accidentally shared

Consider:

  1. Moving sensitive data to POST body parameters
  2. Using encrypted tokens to pass sensitive information
  3. Storing sensitive data in session and using a session token in URLs

Would you like me to help implement a more secure solution?


Line range hint 366-394: Optimize coordinator assignment logic

The coordinator assignment logic makes multiple database queries that could be optimized:

  1. Cache the fallback office lookup
  2. Batch the coordinator queries
  3. Use a single query with proper joins instead of multiple separate queries
+ private static $fallbackOfficeCache = null;
+
  private static function getFallbackOffice() {
+   if (self::$fallbackOfficeCache !== null) {
+     return self::$fallbackOfficeCache;
+   }
+
    $fallbackOffices = Contact::get(FALSE)
      ->addSelect('id')
      ->addWhere('organization_name', 'CONTAINS', self::FALLBACK_OFFICE_NAME)
      ->execute();

-   return $fallbackOffices->first();
+   self::$fallbackOfficeCache = $fallbackOffices->first();
+   return self::$fallbackOfficeCache;
  }
🧹 Nitpick comments (12)
wp-content/civi-extensions/goonjcustom/Civi/InstitutionGoonjActivitiesService.php (7)

387-387: Consider using a class constant for restricted roles.

Moving the roles array to a class constant would improve maintainability and reusability.

+ const RESTRICTED_ROLES = ['account_team', 'ho_account'];
- $restrictedRoles = ['account_team', 'ho_account'];
...
- $hasRestrictedRole = !$isAdmin && \CRM_Core_Permission::checkAnyPerm($restrictedRoles);
+ $hasRestrictedRole = !$isAdmin && \CRM_Core_Permission::checkAnyPerm(self::RESTRICTED_ROLES);

Also applies to: 391-391


389-391: Consider combining permission checks.

The admin check and restricted role check could be combined into a single method for better reusability.

+ private static function hasRestrictedAccess() {
+   return !\CRM_Core_Permission::check('admin') && 
+          \CRM_Core_Permission::checkAnyPerm(self::RESTRICTED_ROLES);
+ }
...
- $isAdmin = \CRM_Core_Permission::check('admin');
- $hasRestrictedRole = !$isAdmin && \CRM_Core_Permission::checkAnyPerm($restrictedRoles);
+ $hasRestrictedRole = self::hasRestrictedAccess();

Line range hint 1-1: Consider adding PHPDoc for the institutionGoonjActivitiesTabset method.

The method lacks documentation explaining its purpose and parameters.

+/**
+ * Manages tab visibility based on user roles for institution Goonj activities.
+ *
+ * @param string $tabsetName The name of the tabset
+ * @param array &$tabs Reference to tabs array
+ * @param array $context Context information
+ */
public static function institutionGoonjActivitiesTabset($tabsetName, &$tabs, $context) {

Also applies to: 387-397


Line range hint 1-1: Add strict types declaration.

Consider adding strict types declaration to enforce type checking.

+declare(strict_types=1);
<?php

Line range hint 1-1: Class lacks proper PHPDoc.

The class is missing proper documentation explaining its purpose and responsibilities.

/**
+ * Service class handling Institution Goonj Activities functionality.
+ *
+ * This class manages activities, permissions, and notifications for
+ * Institution Goonj Activities within the CiviCRM system.
+ */
class InstitutionGoonjActivitiesService extends AutoSubscriber {

Also applies to: 18-18


Line range hint 1-1: Consider using typed properties.

Class properties should use PHP 7.4+ typed properties for better type safety.

- private static $instituteGoonjActivitiesAddress = NULL;
- private static $institutePocAddress = NULL;
- private static $addressAdded = FALSE;
+ private static ?string $instituteGoonjActivitiesAddress = NULL;
+ private static ?string $institutePocAddress = NULL;
+ private static bool $addressAdded = FALSE;

Also applies to: 20-24


Line range hint 1-1: Consider implementing an interface.

The class could benefit from implementing an interface to define its contract.

Consider creating an InstitutionGoonjActivitiesServiceInterface to define the required methods and improve maintainability.

wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php (1)

93-103: Consider refactoring repeated role-check logic into a shared utility.

The lines introduced for removing 'view' and 'edit' tabs if the user has restricted roles are solid. However, this same pattern is repeated across multiple service classes, suggesting duplication. A shared helper or trait to manage these tab restrictions could promote better maintainability.

Example refactor (optional):

-class CollectionCampService extends AutoSubscriber {
+abstract class AbstractCampService extends AutoSubscriber {
+  protected static function hideViewEditTabsForRestrictedRoles(&$tabs) {
+    $restrictedRoles = ['account_team', 'ho_account'];
+    $isAdmin = \CRM_Core_Permission::check('admin');
+    $hasRestrictedRole = !$isAdmin && \CRM_Core_Permission::checkAnyPerm($restrictedRoles);
+    if ($hasRestrictedRole) {
+      unset($tabs['view'], $tabs['edit']);
+    }
+  }
+}

 public static function collectionCampTabset($tabsetName, &$tabs, $context) {
   // ...
-  $restrictedRoles = ['account_team', 'ho_account'];
-  $isAdmin = \CRM_Core_Permission::check('admin');
-  $hasRestrictedRole = !$isAdmin && \CRM_Core_Permission::checkAnyPerm($restrictedRoles);
-  if ($hasRestrictedRole) {
-    unset($tabs['view']);
-    unset($tabs['edit']);
-  }
+  self::hideViewEditTabsForRestrictedRoles($tabs);
   // ...
 }
wp-content/civi-extensions/goonjcustom/Civi/DroppingCenterService.php (1)

560-570: Centralize the restricted role checks.

You are consistently checking for restricted roles, repeating the same code snippet. Extracting this logic into a shared helper method or trait could simplify maintenance and ensure consistency.

wp-content/civi-extensions/goonjcustom/Civi/InstitutionDroppingCenterService.php (1)

618-628: Streamline the role-based tab hiding across services.

The repeated block for verifying admin vs. restricted roles across multiple service classes can be unified. This reduces code duplication and centralizes policy changes.

wp-content/civi-extensions/goonjcustom/Civi/GoonjActivitiesService.php (1)

351-361: Eliminate repeated code for removing tabs.

As with other files, relocating the tab removal logic to a single shared location (e.g., a trait or helper class) would improve maintainability and avoid scattered duplication.

wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php (1)

847-857: LGTM! Consider some minor improvements for maintainability.

The role-based access control implementation looks good. However, here are some suggestions to make it more maintainable and robust:

+ // Define role constants at the class level
+ const ROLE_ACCOUNT_TEAM = 'account_team';
+ const ROLE_HO_ACCOUNT = 'ho_account';
+
  public static function institutionCollectionCampTabset($tabsetName, &$tabs, $context) {
    if (!self::isViewingInstituteCollectionCamp($tabsetName, $context)) {
      return;
    }

-    $restrictedRoles = ['account_team', 'ho_account'];
+    $restrictedRoles = [self::ROLE_ACCOUNT_TEAM, self::ROLE_HO_ACCOUNT];

    $isAdmin = \CRM_Core_Permission::check('admin');
    $hasRestrictedRole = !$isAdmin && \CRM_Core_Permission::checkAnyPerm($restrictedRoles);

-    if ($hasRestrictedRole) {
+    if ($hasRestrictedRole && isset($tabs['view'], $tabs['edit'])) {
       unset($tabs['view']);
       unset($tabs['edit']);
     }

The changes:

  1. Move role strings to constants for better maintainability
  2. Add defensive check for tab existence before unsetting
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 46d835b and 872ee16.

📒 Files selected for processing (6)
  • wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php (1 hunks)
  • wp-content/civi-extensions/goonjcustom/Civi/DroppingCenterService.php (1 hunks)
  • wp-content/civi-extensions/goonjcustom/Civi/GoonjActivitiesService.php (1 hunks)
  • wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php (1 hunks)
  • wp-content/civi-extensions/goonjcustom/Civi/InstitutionDroppingCenterService.php (1 hunks)
  • wp-content/civi-extensions/goonjcustom/Civi/InstitutionGoonjActivitiesService.php (1 hunks)
🔇 Additional comments (1)
wp-content/civi-extensions/goonjcustom/Civi/InstitutionGoonjActivitiesService.php (1)

387-397: LGTM: Role-based access control implementation looks good.

The implementation correctly handles:

  • Defining restricted roles
  • Checking admin permissions
  • Removing view/edit tabs for restricted roles

@tarunnjoshi tarunnjoshi merged commit 01d003c into develop Dec 21, 2024
@tarunnjoshi tarunnjoshi deleted the hide-view-and-edit-tab-for-account-team branch December 21, 2024 08:53
This was referenced Jan 25, 2025
@coderabbitai coderabbitai bot mentioned this pull request Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant