Deprecation warning: Added combined hook at_access(result, accessing_obj, access_type, **kwargs) to Objects and Players. This hook replaces at_access_failure and at_access_success, which are now both DEPRECATED and will be removed later.

This commit is contained in:
Griatch 2014-01-18 23:56:07 +01:00
parent aef7ccec8d
commit 87f3093dac
4 changed files with 60 additions and 18 deletions

View file

@ -122,10 +122,9 @@ class Object(DefaultObject):
at_server_reload() - called before server is reloaded at_server_reload() - called before server is reloaded
at_server_shutdown() - called just before server is fully shut down at_server_shutdown() - called just before server is fully shut down
at_access_success(accessing_obj, access_type) - called if an lock access at_access(result, accessing_obj, access_type) - called with the result
check succeeded on this object of a lock access check on this object. Return value
at_access_failure(accessing_obj, access_type) - called if an lock access does not affect check result.
check failed on this object
at_before_move(destination) - called just before moving object at_before_move(destination) - called just before moving object
to the destination. If returns False, move is cancelled. to the destination. If returns False, move is cancelled.

View file

@ -18,6 +18,7 @@ they control by simply linking to a new object's user property.
from django.conf import settings from django.conf import settings
from src.typeclasses.typeclass import TypeClass from src.typeclasses.typeclass import TypeClass
from src.commands import cmdset, command from src.commands import cmdset, command
from src.utils.logger import log_depmsg
__all__ = ("Object", "Character", "Room", "Exit") __all__ = ("Object", "Character", "Room", "Exit")
@ -155,7 +156,11 @@ class Object(TypeClass):
this object in any fashion this object in any fashion
at_object_receive(obj, source_location) - called when this object at_object_receive(obj, source_location) - called when this object
receives another object receives another object
at_access(result, **kwargs) - this is called with the result of an
access call, along with any kwargs used
for that call. The return of this
method does not affect the result of the
lock check.
at_before_traverse(traversing_object) - (exit-objects only) called at_before_traverse(traversing_object) - (exit-objects only) called
just before an object just before an object
traverses this object traverses this object
@ -417,7 +422,7 @@ class Object(TypeClass):
return self.dbobj.swap_typeclass(new_typeclass, return self.dbobj.swap_typeclass(new_typeclass,
clean_attributes=clean_attributes, no_default=no_default) clean_attributes=clean_attributes, no_default=no_default)
def access(self, accessing_obj, access_type='read', default=False): def access(self, accessing_obj, access_type='read', default=False, **kwargs):
""" """
Determines if another object has permission to access this object in Determines if another object has permission to access this object in
whatever way. whatever way.
@ -425,15 +430,19 @@ class Object(TypeClass):
accessing_obj (Object)- object trying to access this one accessing_obj (Object)- object trying to access this one
access_type (string) - type of access sought access_type (string) - type of access sought
default (bool) - what to return if no lock of access_type was found default (bool) - what to return if no lock of access_type was found
**kwargs - passed to at_access hook along with result,accessing_obj and access_type
This function will call at_access_success or at_access_failure
depending on the outcome of the access check.
""" """
if self.dbobj.access(accessing_obj, access_type=access_type, default=default): result = self.dbobj.access(accessing_obj, access_type=access_type, default=default)
self.at_access(result, accessing_obj, access_type, **kwargs)
return result
# OBS: DEPRECATED!
if result:
log_depmsg("at_access_success is deprecated. Use at_access(result,**kwargs) instead.")
self.at_access_success(accessing_obj, access_type) self.at_access_success(accessing_obj, access_type)
return True return True
else: else:
log_depmsg("at_access_failure is deprecated. Use at_access(result,**kwargs) instead.")
self.at_access_failure(accessing_obj, access_type) self.at_access_failure(accessing_obj, access_type)
return False return False
@ -526,6 +535,7 @@ class Object(TypeClass):
""" """
pass pass
def at_cmdset_get(self): def at_cmdset_get(self):
""" """
Called just before cmdsets on this object are requested by the Called just before cmdsets on this object are requested by the
@ -585,8 +595,20 @@ class Object(TypeClass):
""" """
pass pass
def at_access(self, result, accessing_obj, access_type, **kwargs):
"""
This is called with the result of an access call, along with
any kwargs used for that call. The return of this method does
not affect the result of the lock check. It can be used e.g. to
customize error messages in a central location or other effects
based on the access result.
"""
pass
def at_access_success(self, accessing_obj, access_type): def at_access_success(self, accessing_obj, access_type):
""" """
OBS: DEPRECATED. Use at_access instead
This hook is called whenever accessing_obj succeed a lock check of This hook is called whenever accessing_obj succeed a lock check of
type access_type on this object, for whatever reason. The return value type access_type on this object, for whatever reason. The return value
of this hook is not used, the lock will still pass regardless of what of this hook is not used, the lock will still pass regardless of what
@ -596,6 +618,8 @@ class Object(TypeClass):
def at_access_failure(self, accessing_obj, access_type): def at_access_failure(self, accessing_obj, access_type):
""" """
OBS: DEPRECATED. Use at_access instead
This hook is called whenever accessing_obj fails a lock check of type This hook is called whenever accessing_obj fails a lock check of type
access_type on this object, for whatever reason. The return value of access_type on this object, for whatever reason. The return value of
this hook is not used, the lock will still fail regardless of what this hook is not used, the lock will still fail regardless of what

View file

@ -90,6 +90,7 @@ class Player(TypeClass):
usually handled on the character level: usually handled on the character level:
at_init() at_init()
at_access()
at_cmdset_get() at_cmdset_get()
at_first_login() at_first_login()
at_post_login(sessid=None) at_post_login(sessid=None)
@ -213,12 +214,11 @@ class Player(TypeClass):
Returns: Returns:
boolean True/False depending on if the swap worked or not. boolean True/False depending on if the swap worked or not.
""" """
self.dbobj.swap_typeclass(new_typeclass, self.dbobj.swap_typeclass(new_typeclass,
clean_attributes=clean_attributes, no_default=no_default) clean_attributes=clean_attributes, no_default=no_default)
def access(self, accessing_obj, access_type='read', default=False): def access(self, accessing_obj, access_type='read', default=False, **kwargs):
""" """
Determines if another object has permission to access this object Determines if another object has permission to access this object
in whatever way. in whatever way.
@ -226,8 +226,11 @@ class Player(TypeClass):
accessing_obj (Object)- object trying to access this one accessing_obj (Object)- object trying to access this one
access_type (string) - type of access sought access_type (string) - type of access sought
default (bool) - what to return if no lock of access_type was found default (bool) - what to return if no lock of access_type was found
**kwargs - passed to the at_access hook along with the result.
""" """
return self.dbobj.access(accessing_obj, access_type=access_type, default=default) result = self.dbobj.access(accessing_obj, access_type=access_type, default=default)
self.at_access(result, accessing_obj, access_type, **kwargs)
return result
def check_permstring(self, permstring): def check_permstring(self, permstring):
""" """
@ -236,6 +239,7 @@ class Player(TypeClass):
permstring (string) - permission string that need to match a permission permstring (string) - permission string that need to match a permission
on the object. (example: 'Builders') on the object. (example: 'Builders')
Note that this method does -not- call the at_access hook.
""" """
return self.dbobj.check_permstring(permstring) return self.dbobj.check_permstring(permstring)
@ -288,6 +292,16 @@ class Player(TypeClass):
# and have some things that should be done regardless of which # and have some things that should be done regardless of which
# character is currently connected to this player. # character is currently connected to this player.
def at_access(self, result, accessing_obj, access_type, **kwargs):
"""
This is called with the result of an access call, along with
any kwargs used for that call. The return of this method does
not affect the result of the lock check. It can be used e.g. to
customize error messages in a central location or other effects
based on the access result.
"""
pass
def at_cmdset_get(self): def at_cmdset_get(self):
""" """
Called just before cmdsets on this player are requested by the Called just before cmdsets on this player are requested by the

View file

@ -173,14 +173,17 @@ class Attribute(SharedMemoryModel):
def __unicode__(self): def __unicode__(self):
return u"%s(%s)" % (_GA(self, "db_key"), _GA(self, "id")) return u"%s(%s)" % (_GA(self, "db_key"), _GA(self, "id"))
def access(self, accessing_obj, access_type='read', default=False): def access(self, accessing_obj, access_type='read', default=False, **kwargs):
""" """
Determines if another object has permission to access. Determines if another object has permission to access.
accessing_obj - object trying to access this one accessing_obj - object trying to access this one
access_type - type of access sought access_type - type of access sought
default - what to return if no lock of access_type was found default - what to return if no lock of access_type was found
**kwargs - passed to at_access hook along with result.
""" """
return self.locks.check(accessing_obj, access_type=access_type, default=default) result = self.locks.check(accessing_obj, access_type=access_type, default=default)
self.at_access(result, **kwargs)
return result
def at_set(self, new_value): def at_set(self, new_value):
""" """
@ -1060,19 +1063,21 @@ class TypedObject(SharedMemoryModel):
# Lock / permission methods # Lock / permission methods
# #
def access(self, accessing_obj, access_type='read', default=False): def access(self, accessing_obj, access_type='read', default=False, **kwargs):
""" """
Determines if another object has permission to access. Determines if another object has permission to access.
accessing_obj - object trying to access this one accessing_obj - object trying to access this one
access_type - type of access sought access_type - type of access sought
default - what to return if no lock of access_type was found default - what to return if no lock of access_type was found
**kwargs - this is ignored, but is there to make the api consistent with the
object-typeclass method access, which use it to feed to its hook methods.
""" """
return self.locks.check(accessing_obj, access_type=access_type, default=default) return self.locks.check(accessing_obj, access_type=access_type, default=default)
def check_permstring(self, permstring): def check_permstring(self, permstring):
""" """
This explicitly checks if we hold particular permission without This explicitly checks if we hold particular permission without
involving any locks. involving any locks. It does -not- trigger the at_access hook.
""" """
if hasattr(self, "player"): if hasattr(self, "player"):
if self.player and self.player.is_superuser: if self.player and self.player.is_superuser: