Answers.Body: <p>Here's a quick and dirty drupal-ly way of doing it, but I still wonder about a CiviCRM specific methodology. </p>
  
  <pre><code>function civicrm_custom_access_civicrm_buildForm($formName, &amp;$form) {
    global $user;
  
    $grant_access = 0;
    foreach($user-&gt;roles as $key =&gt; $role_name) {
      if($key == 3 || $key == 5 || $key == 4 || $key == 6 || $key == 7) {
        $grant_access = 1;
      } 
    }
  
    if($formName == 'CRM_Contribute_Form_Contribution_Main' &amp;&amp; $form-&gt;_id == 2 &amp;&amp; !$grant_access) {
      drupal_access_denied();
      drupal_exit();
    }
  
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-08T03:41:06.243Z
Answers.Text: +1 for this approach, which since it uses hooks could also be done in a CiviCRM extension. Pretty much what I was going to suggest - intercept in hook_civicrm_buildForm(), deny access if user access conditions aren't met.
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T21:32:20.740Z
Answers.Text: Sorry if this is a really stupid question -- but where would this code snippet be placed?
Answers.UserDisplayName: Alex Katayama
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-15T14:15:59.733Z
Answers.Text: this bit of code implements hook_civicrm_buildForm().http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm.....In this example my module name was civicrm_custom_access ....So place this bit of code in a small module...with the function name YOUR_MODULE_NAME_civicrm_buildForm()
Answers.UserDisplayName: jackrabbithanna
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T15:24:28.970Z
Answers.LastActivityDate: 2015-04-07T15:24:28.970Z
Answers.UserDisplayName: jackrabbithanna
Body: <p>Drupal 7 / CiviCRM 4.5.8
  Working on a site which has several contribution pages.  We want to leave the majority of them accessible to anonymous users, but have just one that we want to restrict to "members-only". </p>
  
  <p>In Drupal the $form variable passed in hook_form_alter() will have a '#access' parameter that can be set to false. </p>
  
  <p>What's the best way to accomplish this with a CiviCRM form? 
  Is there something that can be modified in the $form variable using hook_civicrm_buildForm()?</p>
  
  <p>Some better way?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-07T16:16:15.073Z
Comments.Text: How is "members-only" defined here?  Is it folks with a current CiviMember record?
Comments.UserDisplayName: Jon G - Megaphone Tech
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-07T16:26:56.923Z
Comments.Text: "Members Only" is based on Drupal user role, I want to know more generally how to programmatically deny access to a single contribution page
Comments.UserDisplayName: jackrabbithanna
Tags: <forms><contribution-page>
Title: How to restrict access to only one Contribution Page
ThreadText: # How to restrict access to only one Contribution Page
  
  (Asked by: username_0 on 2015-04-07)
  
  Drupal 7 / CiviCRM 4.5.8
  Working on a site which has several contribution pages. We want to leave the majority of them accessible to anonymous users, but have just one that we want to restrict to "members-only".
  
  In Drupal the $form variable passed in hook\_form\_alter() will have a '#access' parameter that can be set to false.
  
  What's the best way to accomplish this with a CiviCRM form?
  Is there something that can be modified in the $form variable using hook\_civicrm\_buildForm()?
  
  Some better way?
  
  **Tags:** forms, contribution-page
  
  > **Comments:**
  > **username_1**: How is "members-only" defined here?  Is it folks with a current CiviMember record?  
  > **username_0**: "Members Only" is based on Drupal user role, I want to know more generally how to programmatically deny access to a single contribution page  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-04-07. Score: 3
  
  Here's a quick and dirty drupal-ly way of doing it, but I still wonder about a CiviCRM specific methodology. 
  function civicrm_custom_access_civicrm_buildForm($formName, &$form) {
    global $user;
  
    $grant_access = 0;
    foreach($user->roles as $key => $role_name) {
      if($key == 3 || $key == 5 || $key == 4 || $key == 6 || $key == 7) {
        $grant_access = 1;
      } 
    }
  
    if($formName == 'CRM_Contribute_Form_Contribution_Main' && $form->_id == 2 && !$grant_access) {
      drupal_access_denied();
      drupal_exit();
    }
  
  }
  
  > **Comments:**
  > **username_2**: +1 for this approach, which since it uses hooks could also be done in a CiviCRM extension. Pretty much what I was going to suggest - intercept in hook_civicrm_buildForm(), deny access if user access conditions aren't met.  
  > **username_3**: Sorry if this is a really stupid question -- but where would this code snippet be placed?  
  > **username_0**: this bit of code implements hook_civicrm_buildForm().http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm.....In this example my module name was civicrm_custom_access ....So place this bit of code in a small module...with the function name YOUR_MODULE_NAME_civicrm_buildForm()  
  
  ---
  
  