launchpad-dev team mailing list archive
-
launchpad-dev team
-
Mailing list archive
-
Message #02264
API CHANGE: AuthorizationBase.checkAuthenticated user parameter
Hello fellow Launchpadders,
please make note of this change if you are working on permission
checkers in lib/canonical/launchpad/security.py and for future reference.
As of devel revision 10179 the 'user' parameter of the
'AuthorizationBase.checkAuthenticated' method has changed from being an
'IPerson' object to an 'IPersonRoles' object. The purpose of this change
is to remove repetitive code from security.py and thereby make it
shorter and more readable.
All of the existing checkers have been converted to work with the new
parameter type. If you have a branch in progress that changes
security.py, please merge devel and adapt your changes as described here.
The 'IPersonRoles' object makes checks for membership in celebrity teams
as simple as checking a boolean property. You can read all about it in
lib/canonical/launchpad/doc/personroles.txt but here is the essence:
Instead of:
rosetta_experts = getUtility(ILaunchpadCelebrities).rosetta_experts
if user.inTeam(rosetta_experts):
do_something()
you now simply do:
if user.in_rosetta_experts:
do_something()
Short and clear, isn't it? ;) Also, there is a shorthand for
user.inTeam(self.obj.owner)
which is:
user.isOwner(self.obj)
There is another shorthand for the driver and also IHasDrivers objects
and for checking multiple roles on an object, please see the doctest for
the details.
Additionally, if you need to bail out to old IPerson habits, the
'inTeam' method is provided directly by IPersonRoles, so that
user.inTeam(myteam)
still works as before. Also, the adapted IPerson object is still
available as the 'person' attribute, so you can do
self.obj.canDoSomething(user.person)
As a note: 'IAuthorization' does not include 'checkAuthenticated'
anymore (even before this change), so test code should use
'checkAccountAuthenticated' instead. This will also make sure, that the
checkAuthenticated method will receive the correct type of parameter.
Existing tests that failed because of this have been fixed.
Finally, as 'IPersonRoles' is registered as an adpater for 'IPerson' you
can use it in any other code if you want to check for celebrity teams,
like this:
if IPersonRoles(person).in_rosetta_experts:
do_something()
Thanks for listening, enjoy the new code! ;-)
Henning