class AccessRules(object): def __init__(self, aerospike_connector): self._signs_engine = SignsEngine(aerospike_connector) def check_can_read_sign(self, access_token, sign_id=None, sign_info=None): if access_token.is_admin == True: return True # Retrieve SignInfo if not presented if sign_info == None: sign_info = self._signs_engine.get_info(sign_id) if sign_info.is_private == False or sign_info.user_id == access_token.user_id: return True # Check access table if self._signs_engine.check_access(access_token.user_id, sign_info.sign_id) == True: return True raise APIAccessError('User {} has no read access to sign {}'.format( access_token.user_id, sign_info.sign_id)) def check_can_edit_sign(self, access_token, sign_id=None, sign_info=None): if access_token.is_admin == True: return True # Retrieve SignInfo if not presented if sign_info == None: sign_info = self._signs_engine.get_info(sign_id) if sign_info.user_id == access_token.user_id: return True raise APIAccessError('User {} has no edit access to sign {}'.format( access_token.user_id, sign_info.sign_id)) def check_can_view_private_info(self, access_token, user_id): if access_token.is_admin == True or access_token.user_id == user_id: return True raise APIAccessError( 'User {} has no access to private user info {}'.format( access_token.user_id, user_id)) def check_can_edit_user_info(self, access_token, user_id): if access_token.is_admin == True or access_token.user_id == user_id: return True raise APIAccessError( 'User {} has no access to edit user info {}'.format( access_token.user_id, user_id))
class PutSignSession(POSTSession): def __init__(self, global_context): self._sign_engine = SignsEngine(global_context) self._users_engine = UsersEngine(global_context) self._params = Params() def _init_session_params(self, query): self._params.parse(query) def _run_session(self): logging.info('Put sign, lat = {}, long = {}'.format( self._params.latitude, self._params.longitude)) info = SignInfo() info.user_id = self._params.user_token.user_id info.latitude = self._params.latitude info.longitude = self._params.longitude info.radius = self._params.radius info.time_to_live = self._params.time_to_live info.timestamp = self._params.timestamp info.is_private = self._params.is_private sign_id = self._sign_engine.put_sign(info, \ self._params.features, \ self._params.meta_blob, \ self._params.object_blob, \ self._params.image_blob, \ self._params.preview_blob) self._users_engine.put_sign(info.user_id, sign_id) logging.info('Put succeed, sign_id = {}'.format(sign_id)) return {'success': True, 'sign_id': sign_id}
class RemoveSignSession(POSTSession): def __init__(self, global_context): self._sign_engine = SignsEngine(global_context) self._access_rules = global_context.access_rules self._params = Params() def _init_session_params(self, query): self._params.parse(query) def _run_session(self): # Check user credentials self._access_rules.check_can_edit_sign(self._params.user_token, sign_id=self._params.sign_id) self._sign_engine.remove_sign(self._params.sign_id) return {'success': True}
class GetSignSession(POSTSession): def __init__(self, global_context): self._sign_engine = SignsEngine(global_context) self._access_rules = global_context.access_rules self._params = Params() def _init_session_params(self, query): self._params.parse(query) def _run_session(self): info = self._sign_engine.get_info(self._params.sign_id) # Check user credentials self._access_rules.check_can_read_sign(self._params.user_token, sign_info=info) result = { 'sign_id': info.sign_id, 'user_id': info.user_id, 'latitude': info.latitude, 'longitude': info.longitude, 'radius': info.radius, 'timestamp': info.timestamp, 'time_to_live': info.time_to_live, 'is_private': info.is_private } if 'meta_blob' in self._params.properties: result['meta_blob'] = self._sign_engine.get_meta( self._params.sign_id) if 'object_blob' in self._params.properties: result['object_blob'] = self._sign_engine.get_object( self._params.sign_id) if 'image_blob' in self._params.properties: result['image_blob'] = self._sign_engine.get_image( self._params.sign_id) if 'preview_blob' in self._params.properties: result['preview_blob'] = self._sign_engine.get_preview( self._params.sign_id) return result
class AddSignViewSession(POSTSession): def __init__(self, global_context): self._signs_engine = SignsEngine(global_context) self._users_engine = UsersEngine(global_context) self._access_rules = global_context.access_rules self._params = Params() def _init_session_params(self, query): self._params.parse(query) def _run_session(self): # Check user credentials self._access_rules.check_can_read_sign(self._params.user_token, sign_id=self._params.sign_id) self._signs_engine.add_sign_view(self._params.user_token.user_id, self._params.sign_id) self._users_engine.put_view(self._params.user_token.user_id, self._params.sign_id) return {'success': True}
class UpdateSignSession(POSTSession): def __init__(self, global_context): self._params = Params() self._sign_engine = SignsEngine(global_context) self._access_rules = global_context.access_rules def _init_session_params(self, query): self._params.user_token = query.get_user_token() self._params.sign_id = query.get_required_int64('sign_id') self._params.is_private = query.get_optional_bool('is_private') def _run_session(self): info = self._sign_engine.get_info(self._params.sign_id) # Check user credentials self._access_rules.check_can_edit_sign(self._params.user_token, sign_info=info) if self._params.is_private != None: self._sign_engine.set_sign_privacy(self._params.sign_id, self._params.is_private) return {'success': True}
class CheckSignLikeSession(POSTSession): def __init__(self, global_context): self._signs_engine = SignsEngine(global_context) self._access_rules = global_context.access_rules self._params = Params() def _init_session_params(self, query): self._params.parse(query) def _run_session(self): # Check user credentials self._access_rules.check_can_read_sign(self._params.user_token, sign_id=self._params.sign_id) found = self._signs_engine.check_likes(self._params.user_token.user_id, [self._params.sign_id]) return {'success': True, 'found': found[0]}
class GetUserSignsSession(POSTSession): def __init__(self, global_context): self._users_engine = UsersEngine(global_context) self._signs_enging = SignsEngine(global_context) self._params = Params() def _init_session_params(self, query): self._params.parse(query) def _run_session(self): page = self._users_engine.get_signs(self._params.user_id, self._params.limit, self._params.cursor) signs = list() # Retrieve aux data is_private_list = self._signs_enging.check_privacy_many(page.data) for i in range(len(page.data)): if is_private_list[i] == None: logging.warning('Check privacy for unknown sign {}'.format( page.data[i])) continue if (is_private_list[i] and self._params.privacy == PRIVACY_PUBLIC) \ or (not is_private_list[i] and self._params.privacy == PRIVACY_PRIVATE): continue sign = {'sign_id': page.data[i], 'is_private': is_private_list[i]} signs.append(sign) result = { 'data': signs, 'paging': { 'cursor': page.cursor_code, 'has_next': page.has_next } } return result
class GetSignStatsSession(POSTSession): def __init__(self, global_context): self._sign_engine = SignsEngine(global_context) self._access_rules = global_context.access_rules self._params = Params() def _init_session_params(self, query): self._params.parse(query) def _run_session(self): info = self._sign_engine.get_info(self._params.sign_id) # Check user credentials self._access_rules.check_can_read_sign(self._params.user_token, sign_info=info) result = { 'success': True, 'data': { 'likes_count': info.likes_count, 'views_count': info.views_count } } return result
def __init__(self, global_context): self._params = Params() self._sign_engine = SignsEngine(global_context) self._access_rules = global_context.access_rules
def __init__(self, aerospike_connector): self._signs_engine = SignsEngine(aerospike_connector)
def __init__(self, global_context): self._sign_engine = SignsEngine(global_context) self._users_engine = UsersEngine(global_context) self._params = Params()