def _validate_subscription(self, **kw): """ Validate add/edit subscription for authorized and anonymous users """ if kw['notif_type'] not in self.available_notif_types(kw['location']): raise i18n_exception(ValueError, 'Subscribing to notifications in ' '"${location}" not allowed', location=kw['location']) try: obj = self.getSite().restrictedTraverse(kw['location']) except: raise i18n_exception(ValueError, 'This path is invalid or protected') try: subscription_container = ISubscriptionContainer(obj) except: raise i18n_exception(ValueError, 'Cannot subscribe to this folder') if not kw.get('anonymous', False): n = utils.match_account_subscription(subscription_container, kw['user_id'], kw['notif_type'], kw['lang']) if n is not None: raise i18n_exception(ValueError, 'Subscription already exists') else: #Check if subscription exists for this anonymous subscriber if not is_valid_email(kw.get('email', '')): raise i18n_exception(ValueError, 'Your e-mail address does not appear to be valid.') for id, subscription in subscription_container.list_with_keys(): #Normal subscriptions don't have e-mail if isinstance(subscription, AnonymousSubscription): if (subscription.email == kw['email'] and subscription.notif_type == kw['notif_type'] and subscription.lang == kw['lang']): raise i18n_exception(ValueError, 'Subscription already exists')
def _update(self, portal): notif_tool = portal.getNotificationTool() auth_tool = portal.getAuthenticationTool() admins = auth_tool.search_users('', role='Administrator', rkey=0, skey='name', all_users=True, location='_all_') self.log.debug('Started update in %s' % portal.getId()) for admin in admins: for role in admin.roles: if 'Administrator' in role[0]: user_id = admin.user_id own_site_location = path_in_site(role[1]) this_site_location = relative_object_path(role[1], portal) if own_site_location != this_site_location: self.log.debug('Location %s is probably in a subportal' % own_site_location) continue obj = portal.restrictedTraverse(this_site_location) if match_account_subscription( ISubscriptionContainer(obj), user_id, 'administrative', 'en'): self.log.debug('Subscription for user %s already present ' 'in location %s' %(user_id, this_site_location or '/')) else: notif_tool.add_account_subscription(user_id, this_site_location, 'administrative', 'en', []) self.log.debug('Subscription added for user %s in location %s' %(user_id, this_site_location or '/')) return True
def remove_account_subscription(self, user_id, location, notif_type, lang): obj = self.getSite().restrictedTraverse(location) subscription_container = ISubscriptionContainer(obj) n = utils.match_account_subscription(subscription_container, user_id, notif_type, lang) if n is None: raise ValueError('Subscription not found') subscription_container.remove(n)
def change_user_roles(event): """Update subscriptions when a user's roles were updated: If he now gets the Administrator role, he should be subscribed to administrative notifications. If he loses the role, his subscription should be revoked""" notif_tool = event.context.getNotificationTool() portal = event.context.getSite() if 'Administrator' in event.assigned: if not utils.match_account_subscription(ISubscriptionContainer(event.context), event.user_id, 'administrative', 'en'): notif_tool.add_account_subscription(event.user_id, path_in_site(event.context), 'administrative', 'en', []) if 'Administrator' in event.unassigned: if utils.match_account_subscription(ISubscriptionContainer(event.context), event.user_id, 'administrative', 'en'): notif_tool.remove_account_subscription(event.user_id, path_in_site(event.context), 'administrative', 'en')
def remove_account_subscription(self, user_id, location, notif_type, lang, content_types=None): obj = self.getSite().restrictedTraverse(location) subscription_container = ISubscriptionContainer(obj) n = utils.match_account_subscription(subscription_container, user_id, notif_type, lang, content_types) if n is None: raise ValueError('Subscription not found') subscription_container.remove(n)
def change_user_roles(event): """Update subscriptions when a user's roles were updated: If he now gets the Administrator role, he should be subscribed to administrative notifications. If he loses the role, his subscription should be revoked""" notif_tool = event.context.getNotificationTool() portal = event.context.getSite() if "Administrator" in event.assigned: if not utils.match_account_subscription( ISubscriptionContainer(event.context), event.user_id, "administrative", "en" ): notif_tool.add_account_subscription(event.user_id, path_in_site(event.context), "administrative", "en", []) if "Administrator" in event.unassigned: if utils.match_account_subscription( ISubscriptionContainer(event.context), event.user_id, "administrative", "en" ): notif_tool.remove_account_subscription(event.user_id, path_in_site(event.context), "administrative", "en")
def _update(self, portal): notif_tool = portal.getNotificationTool() auth_tool = portal.getAuthenticationTool() admins = auth_tool.search_users('', role='Administrator', rkey=0, skey='name', all_users=True, location='_all_') self.log.debug('Started update in %s' % portal.getId()) for admin in admins: for role in admin.roles: if 'Administrator' in role[0]: user_id = admin.user_id own_site_location = path_in_site(role[1]) this_site_location = relative_object_path(role[1], portal) if own_site_location != this_site_location: self.log.debug( 'Location %s is probably in a subportal' % own_site_location) continue obj = portal.restrictedTraverse(this_site_location) if match_account_subscription(ISubscriptionContainer(obj), user_id, 'administrative', 'en'): self.log.debug( 'Subscription for user %s already present ' 'in location %s' % (user_id, this_site_location or '/')) else: notif_tool.add_account_subscription( user_id, this_site_location, 'administrative', 'en', []) self.log.debug( 'Subscription added for user %s in location %s' % (user_id, this_site_location or '/')) return True