<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">Here's an example from my CAS module. </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Monaco" size="3" style="font: 11.0px Monaco">        </font><font face="Monaco" size="3" color="#808080" style="font: 11.0px Monaco; color: #808080">// Become user 1 to be able to save profile information</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Monaco" size="3" style="font: 11.0px Monaco">         </font><font face="Monaco" size="3" color="#660000" style="font: 11.0px Monaco; color: #660000">$admin</font><font face="Monaco" size="3" style="font: 11.0px Monaco"> = </font><font face="Monaco" size="3" color="#0000ff" style="font: 11.0px Monaco; color: #0000ff">array</font><font face="Monaco" size="3" style="font: 11.0px Monaco">(</font><font face="Monaco" size="3" color="#008200" style="font: 11.0px Monaco; color: #008200">'uid'</font><font face="Monaco" size="3" style="font: 11.0px Monaco">=&gt; </font><font face="Monaco" size="3" color="#ff0000" style="font: 11.0px Monaco; color: #ff0000">1</font><font face="Monaco" size="3" style="font: 11.0px Monaco">);</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Monaco" size="3" style="font: 11.0px Monaco">         </font><font face="Monaco" size="3" color="#660000" style="font: 11.0px Monaco; color: #660000">$user</font><font face="Monaco" size="3" style="font: 11.0px Monaco"> = user_load(</font><font face="Monaco" size="3" color="#660000" style="font: 11.0px Monaco; color: #660000">$admin</font><font face="Monaco" size="3" style="font: 11.0px Monaco">);</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">If you're operating as a normal user you'll want to save away the value of the current user, become admin , do your save stuff, then become the normal user again. Something like: </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">   global $user;  </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">   $temp_user = $user; </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">   $admin= array('uid' =&gt; 1); </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">   $user = user_load($admin); </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">   ... do some save stuff</span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">   $user = user_load($temp_user); </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">This of course should only be used when you're trying to circumvent permissions checked by user_save() (do you have permissions to modify user roles for example). </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">I thought there used to be code in cron.php to do this, but I think I may just be remembering wrong. Perhaps someone else on the dev list could answer the question of who the cron user executes as. </span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">As I said, not 100% sure this is your problem, but I do know you'll have problems having generic users sync up their roles unless you do this permissions escalation. </span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br class="webkit-block-placeholder"></span></font></div><div><div>On Feb 5, 2008, at 5:46 AM, Neil Goodman wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">No, how do I go about transitioning to user 1 for the save operations?<br><br>Right now I just call the function and I assume the permissions would be of whoever is using the module at the time. How do permissions get determined with things like hook_cron()?<br> <br>Neil Goodman<br><br><div class="gmail_quote">On Feb 4, 2008 10:44 PM, David Metzler &lt;<a href="mailto:metzlerd@metzlerd.com">metzlerd@metzlerd.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <div style=""> Last time I ran into something close to this it was because of permissions.  Are you transitioning to user 1 for the save operations? <div><br></div><div>Might be completely off base.... but that's my experience. <div> <div></div><div class="Wj3C7c"><br><div><div><div><div><div>On Feb 4, 2008, at 5:34 PM, Neil Goodman wrote:</div><br><blockquote type="cite">Hello,<br><br>I'm currently trying to solve a bug report for my module (<a href="http://drupal.org/node/217639" target="_blank">http://drupal.org/node/217639</a>). rallycivic keeps getting a "__clone method called on non-object"  when he runs a function in my module. My module runs through a CiviCRM database and pulls out certain CiviCRM contacts that have membership status. The goal is to get this status synchronized to a Drupal role. There is a static method in the CiviCRM API called CRM_Core_BAO_UFMatch::getUFId(). This method will take a CiviCRM contact id and return its corresponding Drupal uid. I then take this list of uids and pass it to the user_multiple_role_edit() function. This function uses the user_save() function to apply role changes. I think that one of rallcivic's uids is causing an issue with this function. From reading the user_save() function's code I can see that it tries to create a new user if the uid doesn't exist. For some reason I think it is trying to do this on a valid uid, which throws a duplicate error that rallycivic has found in watchdog:<br> <br>"Location        <a href="http://chelmsfordmc.co.uk/admin/settings/civimember_roles/manual_sync" title="http://chelmsfordmc.co.uk/admin/settings/civimember_roles/manual_sync" rel="nofollow" target="_blank">http://chelmsfordmc.co.uk/admin/settings/civimember_roles/manual_sync</a><br> Referrer        <a href="http://chelmsfordmc.co.uk/admin/settings/civimember_roles/manual_sync" title="http://chelmsfordmc.co.uk/admin/settings/civimember_roles/manual_sync" rel="nofollow" target="_blank">http://chelmsfordmc.co.uk/admin/settings/civimember_roles/manual_sync</a><br> Message Duplicate entry '' for key 2 query: INSERT INTO users (uid, created, access) VALUES (404, 1202163268, 1202163268) in /home/chelmsf/public_html/includes/database.mysql.inc on line 172."<br><br>Then the drupal_clone() function is called on a bogus object and throws the clone method error. At least, that is what I think is happening.<br> <br>Has anyone ran into a situation similar to this before?<br><br>Neil Goodman<br></blockquote></div><br></div></div></div></div></div></div></div></blockquote></div><br></blockquote></div><br></body></html>