Expose Flag Service

Shongrunden - October 9, 2007 - 18:56
Project:Flag
Version:6.x-2.x-dev
Component:Miscellaneous
Category:feature request
Priority:normal
Assigned:Amitaibu
Status:closed
Issue tags:services integration
Description

This patch exposes a method to flag content using the flag_content module.

The new method available is:

- flag_content.flag

This patch depends on a patch I submitted to the flag_content module at http://drupal.org/node/173292

This patch file represents creating two new files (flag_content_service.module, flag_content_service.info) in a new “flag_content_service” folder in services/services.

AttachmentSize
flag_content_service.patch2.51 KB

#1

snelson - April 12, 2008 - 02:21
Category:bug report» feature request
Assigned to:Anonymous» snelson

#2

snelson - April 12, 2008 - 22:56
Status:needs work» won't fix

Thanks for this Steve ...

I'm setting this to "won't fix" because we're only including Drupal core functionality in Services core, with the exception of Views, of course. If you or anybody else needs this, please consider making it its own contrib module, or including it inside of the flag content module.

Scott

#3

opensanta - March 21, 2009 - 01:12
Title:Expose Flag Content Service» Expose Flag Service
Version:5.x-1.x-dev» 6.x-1.x-dev
Assigned to:snelson» Anonymous
Status:won't fix» active

I know this was initially about flag_content module, but flag module is well worth supporting.

@Shongrunden: what do you think about adding flag support?

#4

marcingy - March 21, 2009 - 01:14
Status:active» won't fix

See comment #2 - this is not part of core drupal and hence will not be considered as part of the services module. You are more than welcome to create your own flag_service as a stand alone project on d.o or indeed as an extension to the flag module.

Thanks marc

#5

opensanta - March 21, 2009 - 03:58
Project:Services» Flag
Version:6.x-1.x-dev» 6.x-1.x-dev
Status:won't fix» active
Issue tags:-flag integration+services, +services integration

Onward we march, waving our banner high!

#6

opensanta - March 31, 2009 - 12:36
Project:Flag» Services
Version:6.x-1.x-dev» 6.x-1.x-dev
Issue tags:+flag integration

If you're willing to put views support in, then I think it's highly worth considering putting in Flag support. Flag will be on d.o, and it's probably the second most core-worthy module not currently being looked at for D7 yet.

Even if you don't end up putting it in, please let other developers work with you in your issue queue under "feature request" until it is ready to be separated. (fyi, Shongruden doesn't have cvs privileges... yet).

#7

heyrocker - March 31, 2009 - 13:24

The question isn't "Is it worth integrating with Flag?" the question is "Where does the code go?" Services has a standard that says going forward we will only expose core functionality, rather than introducing third-module dependencies into the package. Yes I know Views is in there, that happened early on, and as the maintainers determined the overhead to supporting contrib, it was decided that third-party module integration should become patches in those modules' queues. (I wasn't actually a maintainer at the time, but this is my impression, please correct me if I'm wrong.)

I will ping quicksketch later and see how he feels about a patch to Flag for Services. It wouldn't be hard to write.

One thing that would make the third party integrations easier is if our framework was a little more pluggable likes Views - just create a .inc file and drop it in the right place rather than creating a whole new module as you need to now. I'm looking into this for Deploy and I'll see if I get any ideas.

#8

marcingy - March 31, 2009 - 15:30
Status:active» won't fix

None drupal core functional so this being set back to won't fix see comment #2.

#9

opensanta - April 4, 2009 - 02:19
Project:Services» Flag
Version:6.x-1.x-dev» 6.x-1.x-dev
Status:won't fix» active
Issue tags:-flag integration, -services

Moving to Flag module since quicksketch said he's open to including it.

#10

quicksketch - June 26, 2009 - 20:15
Component:Code» Miscellaneous
Status:active» needs work

Walkah posted a helpful starting module for flag as a service in #503416: Add a Service to Flag, which I marked duplicate.

#11

skyredwang - August 10, 2009 - 03:56

subscribe

#12

Amitaibu - October 6, 2009 - 10:09
Version:6.x-1.x-dev» 6.x-2.x-dev
Status:needs work» needs review

* Added action to "flag/ unflag content"; "Check if a content is flagged".
* Removed white space and fix typo in flag.inc

AttachmentSize
182071-flag-services-12.patch 8.08 KB

#13

quicksketch - October 6, 2009 - 13:50
Status:needs review» needs work

Thanks Amitaibu! This looks pretty good, a couple questions/comments:

- Do we need another permission for "access flag service"? All flag access is already checked per-user, so they can't have access to any flags than they do through the normal site. I'd like to avoid adding such permissions if possible.

- We should use $flag->access() for access checking so that the new hook_flag_access() hooks are fired.

- We should remove all the extra white space at the end of the file.

+ if (module_exists('services')) {
+ // Services module doesn't allow us to place the include file in a
+ // subdirectory, so we include it ourself.
+ include_once $path .'/includes/flag.services.inc';
+ }

This is a good fix for the time being, but it'd be good to get a patch submitted to services if it has an autoloading mechanism that we can improve.

#14

Amitaibu - October 6, 2009 - 14:20
Assigned to:Anonymous» Amitaibu
Status:needs work» needs review

> Do we need another permission for "access flag service"

You are right. Services allows creating access control through the user of security keys, where the admin decides which services are available. In other words - removed.

> We should use $flag->access()

Right, forgot about it. Done.

[EDIT: removed wrong sentence]

> but it'd be good to get a patch submitted to services

#597206: Allow modules to include a service file ina subdirectory, although services commit rate is very slow... :/

AttachmentSize
182071-flag-services-14.patch 7.51 KB

#15

marcingy - October 15, 2009 - 04:01

As an fyi http://drupal.org/node/597206 is now committed :)

#16

Amitaibu - October 15, 2009 - 08:44

Thanks marcingy.
re-rolled patch with services change.

AttachmentSize
182071-flag-services-16.patch 7.86 KB

#17

quicksketch - October 28, 2009 - 01:50

I tried out this patch but I couldn't get it working properly. I updated it to remove unnecessary changes (whitespace, plus changes to the install file) that have already been fixed. Regardless, it now needs to be updated to take into account session IDs added by #271582: Allow anonymous users to flag content.

#18

quicksketch - October 28, 2009 - 01:50
Status:needs review» needs work

#19

Amitaibu - October 28, 2009 - 08:16
Status:needs work» needs review

oops, last patch was missing an inc file...
Here's a re-roll. Is there really a special thing I need to do about anonymous sessions?

I've also attached screenshots on how to setup Services, as it cab be a bit tricky.

AttachmentSize
182071-flag-services-19.patch 6.6 KB
Snap1.png 49.1 KB
Snap2.png 40.67 KB
Snap3.png 28.37 KB
Snap4.png 44.69 KB

#20

quicksketch - October 28, 2009 - 15:20

Here's a re-roll. Is there really a special thing I need to do about anonymous sessions?

Another parameter has been added to the flag() function (and most other functions) for the SID (session ID) which helps Flag discriminate between multiple anonymous users, since they all have the UID 0. This $sid parameter should be exposed in Services so that anonymous users can Flag through services.

#21

quicksketch - October 29, 2009 - 03:16

Thanks Amitaibu, those screenshots definitely helped. I realized I forgot to include my reroll. There were some formatting things I fixed in this version (not a fan of the even-indentation in the services hooks). Following along the screenshots I could get it working great.

Regarding the anonymous SIDs, I realized that we don't actually have the option to pass in an SID at all. I'm not sure that's really a problem though (it'll use the current anonymous user).

So I think this is ready to go, I'll give you another once-over Amitaibu.

AttachmentSize
flag_service.patch 5.56 KB

#22

quicksketch - October 29, 2009 - 03:17

Ah, I just fixed one more problem where access should be denied if trying to register a flag on behalf of a user that does not exist.

AttachmentSize
flag_service.patch 5.58 KB

#23

Amitaibu - October 30, 2009 - 13:52
Status:needs review» needs work

I think the account check is redundent, as we already do it in flag_service_flag_access()

+    if ($uid) {
+      $account = user_load($uid);
+    }
+    else {
+      global $user;
+      $account = $user;
+    }
+
+    if (empty($account) || !$flag->access($content_id, $action, $account)) {
+      // User has no permission to use this flag.
+      return FALSE;

(i.e. the if (empty($account) part)

In #19 I have changed to:

+function flag_service_is_flagged($flag_name, $content_id, $uid = NULL) {
+  $flag = flag_get_flag($flag_name);
+  return $flag->is_flagged($content_id, $uid);
+}

I think it won't return a PHP notice if $uid == NULL (I saw it somewhere in core doin the same).

p.s. took me long time to write this comment as my 10 days old baby is sleeping on me :D

#24

quicksketch - October 30, 2009 - 15:42

p.s. took me long time to write this comment as my 10 days old baby is sleeping on me :D

Ha, congratulations!!

The if (empty($account) is to ensure that an account actually is loaded, $account = user_load($uid); can fail if $uid does not exist, which was causing all kinds of PHP errors. We probably shouldn't use the current user if a $uid was intended to be passed in.

#25

Amitaibu - October 30, 2009 - 16:21

I think I don't get your point. UID is an optional paramter, and if not supplied then we use the current user -- this is the behaviour we wish to preserve.

If the account didn't load properly, maybe because user doesn't exist, then services will return access denied. Can you please re-explain where you think it's not working as should?

#26

quicksketch - October 30, 2009 - 16:30

Say you're user #1, and using the service API you're trying to register a flag on behalf of user #2. So $uid = 2.

+    if ($uid) {
+      $account = user_load($uid);
+    }

Unfortunately user ID 2 *never* exists on a Drupal site, so this call sets $account = NULL. Now instead of registering a vote on user 2, $account is NULL and so the flag will be registered from user 1, when access should have been denied.

#27

Amitaibu - October 30, 2009 - 16:33

But access will be denied:

+    if (empty($account) || !$flag->access($content_id, $action, $account)) {
+      // User has no permission to use this flag.
+      return FALSE;

Account is NULL so we return FALSE and that's where it ends - it doesn't continue and try to flag the content.

#28

quicksketch - October 30, 2009 - 18:41

Account is NULL so we return FALSE and that's where it ends - it doesn't continue and try to flag the content.

Er, right. I'd been explaining why we have the if (empty($account)) part, since you said, "I think the account check is redundent, as we already do it in flag_service_flag_access(). (i.e. the if (empty($account) part)".

#29

Amitaibu - October 30, 2009 - 20:20

sorry, my bad, I didn't explain myself properly.

The redundent part is

+function flag_service_flag($flag_name, $content_id, $uid = NULL, $action = 'flag', $skip_permission_check = FALSE) {
+  global $user;
+  if (!empty($uid)) {
+    if (!($account = user_load($uid))) {
+      // User was not loaded.
+      return FALSE; // <----- we don't need this part.
+    }
+  }
+  else {
+    $account = $user;
+  }

We don't have to recheck the account exists, as we already did it in flag_service_flag_access()

#30

quicksketch - November 2, 2009 - 06:50
Status:needs work» needs review

Ah, right! Now that makes perfect sense!

Here's a reroll that greatly simplifies flag_service_flag(). I also realized that there's no reason to put the access callback in the .module file, since the .inc is pulled in by Services module to call the access callback (which is nice). So now the only thing in the .module file is the hook_service(), everything else is in flag.services.inc.

AttachmentSize
flag_service.patch 5.51 KB

#31

Amitaibu - November 2, 2009 - 07:40
Status:needs review» reviewed & tested by the community

Tested and working.
One minor question I'm not sure:

flag_service_is_flagged($flag_name, $content_id, $uid = NULL) {
  ...
  // Prevent PHP notice in case we are calling user ID if it wasn't supplied.
  return !empty($uid) ? $flag->is_flagged($content_id, $uid) : $flag->is_flagged($content_id);

But maybe since we have $uid = NULL in the function's signature we can safely?

  return $flag->is_flagged($content_id, $uid);

#32

quicksketch - November 2, 2009 - 16:11

But maybe since we have $uid = NULL in the function's signature we can safely?

I imagine this was because of the way Services calls its callbacks. It should work without the conditional there, but I wasn't sure if Services was passing something in even though the parameter was optional.

#33

quicksketch - November 2, 2009 - 21:21
Status:reviewed & tested by the community» fixed

Well I removed that conditional and I couldn't find any problems with it's removal. Removed the conditional and committed. Yay, one patch closer to a 2.x beta!

#34

System Message - November 16, 2009 - 21:30
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.