Skip to content

Commit

Permalink
Merge pull request #58 from oasiswork/identity
Browse files Browse the repository at this point in the history
Add CRUD for Identity
  • Loading branch information
Tuxem authored Sep 22, 2016
2 parents 41266fe + be1f2f7 commit ef7c0b0
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 51 deletions.
84 changes: 43 additions & 41 deletions tests/test_zimbra_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,49 @@ def testGetAPreference(self):
self.assertIsInstance(pref, dict)
self.assertEqual(pref['name'], 'zimbraPrefMailFlashTitle')

def testGetIdentities(self):
identities = self.zc.request('GetIdentities')

# only one
self.assertIsInstance(identities['identity'], dict)

def modifyIdentity(self):
self.zc.request('ModifyIdentity', {'identity': {
'name': 'DEFAULT',
'a': {'name': 'zimbraPrefSaveToSent', '_content': 'FALSE'}
}})

self.zc.request('ModifyIdentity', {'identity': {
'name': 'DEFAULT',
'a': {'name': 'zimbraPrefSaveToSent', '_content': 'TRUE'}
}})

# just checks that it succeeds
def testCreateGetModifyDeleteIdentity(self):
# Create
i = self.zc.create_identity(name='test-identity', attrs=[{
'name': 'zimbraPrefWhenInFoldersEnabled',
'_content': 'TRUE'
}])

# Get
get_i = self.zc.get_identities(identity='test-identity')[0]
# Verify create and get
self.assertEqual(i, get_i)

# Modify 1
from_addr = '[email protected]'

i = self.zc.modify_identity(
identity='test-identity', zimbraPrefFromAddress=from_addr)
self.assertEqual(i._a_tags['zimbraPrefFromAddress'], from_addr)

# Modify 2
# clean (needed with use of zobjects.Identity to avoid illegal
# multivalue attribute)
i._full_data['a'].remove({
'name': 'zimbraPrefFromAddress',
'_content': from_addr})
from_addr = '[email protected]'
i._full_data['a'].append({
'name': 'zimbraPrefFromAddress',
'_content': from_addr})
mod_i = self.zc.modify_identity(i)
self.assertEqual(mod_i._a_tags['zimbraPrefFromAddress'], from_addr)

# Delete 1
self.zc.delete_identity(mod_i)

self.assertEqual(self.zc.get_identities(identity=mod_i), [])

# Delete 2
i = self.zc.create_identity(name='test-identity', attrs={
'zimbraPrefWhenInFoldersEnabled': 'TRUE'})
self.zc.delete_identity(identity='test-identity')

self.assertEqual(self.zc.get_identities(i), [])

def testAddRemoveGetBlackWhiteLists(self):
addr = '[email protected]'
Expand Down Expand Up @@ -338,29 +363,6 @@ def test_get_identities(self):
self.assertEqual(identities[0].name, 'DEFAULT')
self.assertTrue(utils.is_zuuid(identities[0]['zimbraPrefIdentityId']))

def test_modify_identity(self):
test_attr = 'zimbraPrefForwardReplyPrefixChar'

# First get the default identity id
def_identity = self.zc.get_identities()[0]

initial_attrval = def_identity[test_attr]
if initial_attrval == '>':
new_attrval = '|'
else:
new_attrval = '>'

i = Identity(id=def_identity.id)
i[test_attr] = new_attrval
self.zc.modify_identity(i)

modified_i = self.zc.get_identities()[0]
self.assertEqual(modified_i[test_attr], new_attrval)

# Revert it back
i[test_attr] = initial_attrval
self.zc.modify_identity(i)

def test_account_get_logged_in_by(self):
admin_zc = ZimbraAdminClient(TEST_CONF['host'],
TEST_CONF['admin_port'])
Expand Down
77 changes: 72 additions & 5 deletions zimsoap/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,26 @@ def get_preference(self, pref_name):
resp = self.request_single('GetPrefs', {'pref': {'name': pref_name}})
return utils.auto_type(resp['_content'])

def get_identities(self):
""" Get all the identities of the user, as a list
def create_identity(self, name, attrs=[]):
""" Create an Identity
:param: name identity name
:param: attrs list of dict of attributes (zimsoap format)
:returns: a zobjects.Identity object
"""
params = {
'name': name,
'a': attrs
}
resp = self.request('CreateIdentity', {'identity': params})
return zobjects.Identity.from_dict(resp['identity'])

def get_identities(self, identity=None, attrs=None):
""" Get identities matching name and attrs
of the user, as a list
:param: zobjects.Identity or identity name (string)
:param: attrs dict of attributes to return only identities matching
:returns: list of zobjects.Identity
"""
resp = self.request('GetIdentities')
Expand All @@ -434,19 +451,69 @@ def get_identities(self):
if type(identities) != list:
identities = [identities]

return [zobjects.Identity.from_dict(i) for i in identities]
if identity or attrs:
wanted_identities = []

for u_identity in [
zobjects.Identity.from_dict(i) for i in identities]:
if identity:
if isinstance(identity, zobjects.Identity):
if u_identity.name == identity.name:
return [u_identity]
else:
if u_identity.name == identity:
return [u_identity]

elif attrs:
for attr, value in attrs.items():
if (attr in u_identity._a_tags and
u_identity._a_tags[attr] == value):
wanted_identities.append(u_identity)
return wanted_identities
else:
return [zobjects.Identity.from_dict(i) for i in identities]
else:
return []

def modify_identity(self, identity):
def modify_identity(self, identity, **kwargs):
""" Modify some attributes of an identity or its name.
:param: identity a zobjects.Identity with `id` set (mandatory). Also
set items you want to modify/set and/or the `name` attribute to
rename the identity.
Can also take the name in string and then attributes to modify
:returns: zobjects.Identity object
"""
self.request('ModifyIdentity', {'identity': identity.to_creator()})

if isinstance(identity, zobjects.Identity):
self.request('ModifyIdentity', {'identity': identity._full_data})
return self.get_identities(identity=identity.name)[0]
else:
attrs = []
for attr, value in kwargs.items():
attrs.append({
'name': attr,
'_content': value
})
self.request('ModifyIdentity', {
'identity': {
'name': identity,
'a': attrs
}
})
return self.get_identities(identity=identity)[0]

def delete_identity(self, identity):
""" Delete an identity from its name or id
:param: a zobjects.Identity object with name or id defined or a string
of the identity's name
"""
if isinstance(identity, zobjects.Identity):
self.request(
'DeleteIdentity', {'identity': identity.to_selector()})
else:
self.request('DeleteIdentity', {'identity': {'name': identity}})
# Whitelists and Blacklists

def get_white_black_lists(self):
Expand Down
32 changes: 27 additions & 5 deletions zimsoap/zobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,9 @@ class FilterRule(ZObject):


class Identity(ZObject):
"""An account object
"""An identity object
"""
SELECTORS = ('name', 'id')
TAG_NAME = 'identity'
ATTRNAME_PROPERTY = 'name'

Expand All @@ -345,10 +346,13 @@ def to_creator(self):
if hasattr(self, prop):
o[prop] = getattr(self, prop)

if len(self._a_tags) > 0:
o['a'] = []
for node in self._unparse_a_tags(self._a_tags):
o['a'].append(node)
try:
if len(self.a) > 0:
o['a'] = []
for node in self._unparse_a_tags(self._a_tags):
o['a'].append(node)
except AttributeError:
pass
return o

def is_default(self):
Expand All @@ -357,6 +361,24 @@ def is_default(self):
# changed...
return self.name == 'DEFAULT'

def to_selector(self):
""" For some reason, the selector for <identity> is
<identity id="1234" />
rather than
<identity by="id"></identity>
"""

for i in self.SELECTORS:
if hasattr(self, i):
val = getattr(self, i)
selector = i
break

return {selector: val}


class ClassOfService(ZObject):
""" Represents a Class of Service (COS)
Expand Down

0 comments on commit ef7c0b0

Please sign in to comment.