From cac20a353bda562edb1862102911ce249e8ba5b7 Mon Sep 17 00:00:00 2001
From: Greg Anderson <greg.1.anderson@greenknowe.org>
Date: Tue, 25 Sep 2012 11:57:44 -0700
Subject: Issue #697856 by Greg Anderson, Matthew Davidson, bcmiller0, tedfordgif, zserno: Introduce new permission: 'administer content profiles'

---
 content_profile.install |   21 +++++++++++++++++++++
 content_profile.module  |   39 ++++++++++++++++++++++++++++++---------
 2 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/content_profile.install b/content_profile.install
index 2b411ce..c27fa45 100644
--- a/content_profile.install
+++ b/content_profile.install
@@ -136,3 +136,24 @@ function content_profile_update_6004() {
   }
   return $ret;
 }
+
+/**
+ * Give any users with 'administer nodes' permission the new 
+ * 'administer content profiles' permission.
+ */
+function content_profile_update_6005() {
+  $ret = array();
+  $admins = user_roles(TRUE, 'administer users');
+  $result = db_query('SELECT * FROM {role}');
+  while ($role = db_fetch_object($result)) {
+    if (in_array($role->name, $admins)) {
+      $permissions = db_fetch_object(db_query('SELECT * FROM {permission} WHERE rid = %d', $role->rid));
+      $permissions = explode(', ', $permissions->perm);
+      $permissions[] = 'administer content profiles';
+      $ret[] = update_sql('DELETE FROM {permission} WHERE rid = '. $role->rid);
+      $ret[] = update_sql("INSERT INTO {permission} (rid, perm) VALUES ($role->rid, '". implode(', ', $permissions) ."')");
+    }
+  }
+
+  return $ret;
+}
diff --git a/content_profile.module b/content_profile.module
index fb1026d..4fc45bd 100644
--- a/content_profile.module
+++ b/content_profile.module
@@ -8,6 +8,13 @@
 require_once dirname(__FILE__) . '/content_profile.theme_vars.inc';
 
 /**
+ * Implementation of hook_perm().
+ */
+ function content_profile_perm() {
+  return array('administer content profiles');
+}
+
+/**
  * Implementation of hook_ctools_plugin_directory().
  */
 function content_profile_ctools_plugin_directory($module, $plugin) {
@@ -34,7 +41,7 @@ function content_profile_menu() {
       'page callback' => 'drupal_get_form',
       'page arguments' => array('content_profile_admin_settings', $type),
       'access callback' => 'user_access',
-      'access arguments' => array('administer nodes'),
+      'access arguments' => array('administer content profiles'),
       'type' => MENU_LOCAL_TASK,
       'weight' => 1,
     );
@@ -83,11 +90,7 @@ function content_profile_page_access($type, $account) {
   }
   // Else user may view the page when they are going to create their own profile
   // or have permission to create it for others.
-  global $user;
-  if ($user->uid == $account->uid || user_access('administer nodes') ){
-    return node_access('create', $type);
-  }
-  return FALSE;
+  return user_access('administer content profiles') || node_access('create', $type);
 }
 
 /**
@@ -275,6 +278,24 @@ function content_profile_form_alter(&$form, $form_state, $form_id) {
     if (!empty($_GET['uid']) && ($uid = intval($_GET['uid'])) && ($user = user_load($uid))) {
       $form['author']['name']['#default_value'] = $user->name;
     }
+    if (user_access('administer content profiles') || node_access('create', $form['#node']->type)) {
+      $form['author']['#access'] = TRUE;
+      $form['#submit'] = array_merge(array('content_profile_form_submit'), (array)$form['#submit']);
+    }
+  }
+}
+
+/**
+ * Special submit handler for users with 'administer content profiles' role.
+ */
+function content_profile_form_submit($form, &$form_state) {
+  if (user_access('administer content profiles') || node_access('create', $form['#node']->type)) {
+    if ($account = user_load(array('name' => $form_state['values']['name']))) {
+      $form_state['values']['uid'] = $account->uid;
+    }
+    else {
+      $form_state['values']['uid'] = 0;
+    }
   }
 }
 
@@ -369,14 +390,14 @@ function _content_profile_node_delete($node) {
  */
 function content_profile_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
 
-  if ($op == 'prepare' && is_content_profile($node) && !isset($node->nid) && $node->uid && !user_access('administer nodes') && arg(0) != 'admin') {
+  if ($op == 'prepare' && is_content_profile($node) && !isset($node->nid) && $node->uid && !(user_access('administer content profiles') || node_access('create', $node->type)) && arg(0) != 'admin') {
     // Check if this nodetype already exists
     if ($nid = content_profile_profile_exists($node, $node->uid)) {
       // This node already exists, redirect to edit page
       drupal_goto('node/'. $nid .'/edit', 'destination=user/'. $node->uid);
     }
   }
-  elseif ($op == 'validate' && is_content_profile($node) && user_access('administer nodes')) {
+  elseif ($op == 'validate' && is_content_profile($node) && (user_access('administer content profiles') || node_access('create', $node->type))) {
     $form = $a3;
     // Only validate if the user-name changed or we add a new node
     if (!empty($node->nid) && $form['author']['name']['#default_value'] == $node->name) {
@@ -531,7 +552,7 @@ function content_profile_show_profiles($uid) {
       // Working around the bug described at http://drupal.org/node/302873
       module_load_include('inc', 'content_profile', 'content_profile.theme');
     }
-    elseif (user_access('create '. $type .' content') && content_profile_get_settings($type, 'add_link') && !$node && ($uid == $user->uid || user_access('administer nodes'))) {
+    elseif (user_access('create '. $type .' content') && content_profile_get_settings($type, 'add_link') && !$node && ($uid == $user->uid || user_access('administer content profiles'))) {
       $content['content_profile_'. $type] = array(
         '#admin' => $uid != $user->uid,
         '#theme' => 'content_profile_display_add_link',
-- 
1.7.1

