Commit 57a0f6b5 authored by Alain Takoudjou's avatar Alain Takoudjou Committed by Rafael Monnerat

Erp5-Alarm: Check and change wrong allocation scope on public and friend computer

parent fc29856b
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_body</string> </key>
<value> <string>portal = context.getPortalObject()\n
\n
category_public = portal.restrictedTraverse("portal_categories/allocation_scope/open/public", None)\n
category_friend = portal.restrictedTraverse("portal_categories/allocation_scope/open/friend", None)\n
\n
if category_public is not None:\n
portal.portal_catalog.searchAndActivate(\n
portal_type=\'Computer\',\n
default_allocation_scope_uid=[category_public.getUid(), category_friend.getUid()],\n
validation_state="validated",\n
method_id=\'Computer_checkAndUpdateAllocationScope\',\n
activate_kw={\'tag\': tag}\n
)\n
context.activate(after_tag=tag).getId()\n
</string> </value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>tag, fixit, params</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Alarm_checkAndUpdateComputerAllocationScope</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_body</string> </key>
<value> <string encoding="cdata"><![CDATA[
from DateTime import DateTime\n
\n
computer = context\n
portal = context.getPortalObject()\n
allocation_scope = computer.getAllocationScope()\n
\n
if allocation_scope != \'open/public\' and \\\n
allocation_scope != \'open/friend\':\n
return\n
\n
is_service_provider = False\n
person = computer.getSourceAdministrationValue(portal_type="Person")\n
\n
for assignment in person.contentValues(portal_type="Assignment"):\n
assignment = assignment.getObject()\n
if assignment.getRole() == \'service_provider\':\n
is_service_provider = True\n
break\n
\n
if not is_service_provider:\n
#Turn this computer allocation scope to \'open/personal\'\n
edit_kw = {\n
\'allocation_scope\': \'open/personal\',\n
\'subject_list\': [\'\'],\n
\'destination_section_value\': person,\n
} \n
computer.edit(**edit_kw)\n
\n
# Create a ticket (or re-open it) for this issue!\n
ticket_title = \'Allocation scope has been changed for %s\' % computer.\\\n
getReference()\n
message = """Dear user,\n
\n
Your computer %s with ID %s was configured with allocation scope \'%s\', but you are\n
not allowed to turn computer to \'public\' or \'friend\' state because you are not a service provider.\n
The computer allocation scope has been changed to \'open/personal\'. Please contact us for more informations.\n
\n
Regards,\n
\n
SlapOS Team\n
""" % (computer.getTitle(), computer.getReference(),\n
allocation_scope)\n
ressource = portal.service_module.slapos_crm_information.getRelativeUrl()\n
\n
support_request_in_progress = portal.portal_catalog.getResultValue(\n
portal_type = \'Support Request\',\n
title = ticket_title,\n
simulation_state = ["validated","submitted","suspended"],\n
source_project_uid = computer.getUid()\n
)\n
if support_request_in_progress is None:\n
support_request = portal.support_request_module.\\\n
slapos_crm_support_request_template.\\\n
Base_createCloneDocument(batch_mode=1)\n
support_request.edit(\n
title = ticket_title,\n
resource = ressource,\n
description = ticket_title,\n
start_date = DateTime(),\n
destination_decision=computer.getSourceAdministration(),\n
source_project_value = computer.getRelativeUrl()\n
)\n
support_request.validate()\n
support_request.suspend()\n
else:\n
support_request = support_request_in_progress.getObject()\n
\n
# create Web message if needed for this ticket\n
event_list = context.portal_catalog(follow_up_uid=support_request.getUid(), \n
sort_on=[(\'delivery.start_date\', \'DESC\')],\n
)\n
if len(event_list) > 0 and \\\n
(event_list[0].getStartDate() - DateTime() < 1):\n
# User has already been notified this day.\n
return\n
event = portal.event_module.slapos_crm_web_message_template.\\\n
Base_createCloneDocument(batch_mode=1)\n
event.edit(\n
title=\'We have changed allocation scope for %s\' % computer.getReference(),\n
text_content=message,\n
start_date = DateTime(),\n
resource = ressource,\n
source=person.getRelativeUrl(),\n
follow_up=support_request.getRelativeUrl(),\n
)\n
event.stop()\n
event.deliver()\n
]]></string> </value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Computer_checkAndUpdateAllocationScope</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
# Copyright (c) 2012 Nexedi SA and Contributors. All Rights Reserved.
from Products.SlapOS.tests.testSlapOSMixin import testSlapOSMixin
from AccessControl import getSecurityManager
from DateTime import DateTime
import transaction
class TestAllocationScope(testSlapOSMixin):
def afterSetUp(self):
super(TestAllocationScope, self).afterSetUp()
self.user_id = getSecurityManager().getUser().getId()
def beforeTearDown(self):
transaction.abort()
def generateNewId(self):
return self.getPortalObject().portal_ids.generateNewId(
id_group=('slapos_core_test'))
def test_GroupCompany(self):
computer = self.portal.computer_module.newContent(portal_type='Computer')
computer.updateLocalRolesOnSecurityGroups()
self.assertSecurityGroup(computer,
['G-COMPANY', self.user_id], False)
self.assertRoles(computer, 'G-COMPANY', ['Assignor'])
self.assertRoles(computer, self.user_id, ['Owner'])
def test_ComputerAgent(self):
reference = 'TESTPERSON-%s' % self.generateNewId()
person = self.portal.person_module.newContent(portal_type='Person',
reference=reference)
computer = self.portal.computer_module.newContent(portal_type='Computer',
source_administration=person.getRelativeUrl())
computer.updateLocalRolesOnSecurityGroups()
self.assertSecurityGroup(computer,
[self.user_id, 'G-COMPANY', reference], False)
self.assertRoles(computer, reference, ['Assignee'])
self.assertRoles(computer, self.user_id, ['Owner'])
def test_NotAllowedAllocationScope(self):
reference = 'TESTPERSON-%s' % self.generateNewId()
computer_ref = 'TESTCOMP-%s' % self.generateNewId()
person = self.portal.person_module.newContent(portal_type='Person',
reference=reference)
assignment = person.newContent(portal_type='Assignment')
assignment.setRole('role/member')
assignment.setStartDate(DateTime())
assignment.open()
assignment.updateLocalRolesOnSecurityGroups()
computer = self.portal.computer_module.newContent(portal_type='Computer',
source_administration=person.getRelativeUrl(),
reference=computer_ref)
# open/public
computer.edit(allocation_scope='open/public')
computer.updateLocalRolesOnSecurityGroups()
computer.Computer_checkAndUpdateAllocationScope()
self.assertEquals(computer.getAllocationScope(), 'open/personal')
# open/personal
computer.edit(allocation_scope='open/personal',
source_administration=person.getRelativeUrl()
)
computer.updateLocalRolesOnSecurityGroups()
computer.Computer_checkAndUpdateAllocationScope()
self.assertEquals(computer.getAllocationScope(), 'open/personal')
# open/friend
friend_reference = 'TESTPERSON-%s' % self.generateNewId()
friend_person = self.portal.person_module.newContent(portal_type='Person',
reference=friend_reference)
computer.edit(allocation_scope='open/friend',
destination_section=friend_person.getRelativeUrl()
)
computer.updateLocalRolesOnSecurityGroups()
computer.Computer_checkAndUpdateAllocationScope()
self.assertEquals(computer.getAllocationScope(), 'open/personal')
def test_AllowedAllocationScope(self):
# Test computer allocation scope with 'service_provider' user assignment
reference = 'TESTPERSON-%s' % self.generateNewId()
person = self.portal.person_module.newContent(portal_type='Person',
reference=reference)
assignment = person.newContent(portal_type='Assignment')
assignment.setRole('role/service_provider')
assignment.setStartDate(DateTime())
assignment.open()
assignment.updateLocalRolesOnSecurityGroups()
computer = self.portal.computer_module.newContent(portal_type='Computer',
source_administration=person.getRelativeUrl())
# open/public
computer.edit(allocation_scope='open/public')
computer.updateLocalRolesOnSecurityGroups()
computer.Computer_checkAndUpdateAllocationScope()
self.assertEquals(computer.getAllocationScope(), 'open/public')
# open/personal
computer.edit(allocation_scope='open/personal',
source_administration=person.getRelativeUrl()
)
computer.updateLocalRolesOnSecurityGroups()
computer.Computer_checkAndUpdateAllocationScope()
self.assertEquals(computer.getAllocationScope(), 'open/personal')
# open/friend
friend_reference = 'TESTPERSON-%s' % self.generateNewId()
friend_person = self.portal.person_module.newContent(portal_type='Person',
reference=friend_reference)
computer.edit(allocation_scope='open/friend',
destination_section=friend_person.getRelativeUrl()
)
computer.updateLocalRolesOnSecurityGroups()
computer.Computer_checkAndUpdateAllocationScope()
self.assertEquals(computer.getAllocationScope(), 'open/friend')
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment