Skip to content

Commit 44267de

Browse files
committed
begin refactoring UserAction
clean up and simplify constructor move identity type stuff to create - the only operation requiring identity type knowledge
1 parent ece9fac commit 44267de

1 file changed

Lines changed: 15 additions & 54 deletions

File tree

umapi_client/functional.py

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class UserAction(Action):
5555
A sequence of commands to perform on a single user.
5656
"""
5757

58-
def __init__(self, id_type=IdentityTypes.adobeID, email=None, username=None, domain=None, **kwargs):
58+
def __init__(self, user=None, domain=None, use_adobe_id=False, **kwargs):
5959
"""
6060
Create an Action for a user identified either by email or by username and domain.
6161
You should pretty much always use just email, unless the user has a Federated ID and his
@@ -70,58 +70,22 @@ def __init__(self, id_type=IdentityTypes.adobeID, email=None, username=None, dom
7070
will be used to break ties if there is both an AdobeID and an EnterpriseID or FederatedID user
7171
with that same email. Normally, we choose Enterprise ID or Federated ID *over* Adobe ID, but
7272
if you specify the type as Adobe ID then we will act on the Adobe ID user instead.
73-
:param id_type: IdentityTypes enum value (or the name of one), defaults to adobeID
74-
:param email: The user's email. Typically this is also the user's username.
75-
:param username: The username on the Adobe side. If it's the same as email, it's ignored.
76-
:param domain: string, needed only if you specified a username but NOT an email.
73+
:param user: Primary user identifier. Should be username for "create" operations or email address otherwise
74+
:param domain: Domain of non-email username
7775
:param kwargs: other key/value pairs for the action, such as requestID
7876
"""
79-
if id_type in IdentityTypes.__members__:
80-
id_type = IdentityTypes[id_type]
81-
if id_type not in IdentityTypes:
82-
raise ArgumentError("Identity type (%s) must be one of %s" % (id_type, [i.name for i in IdentityTypes]))
83-
self.id_type = id_type
84-
self.email = None
85-
self.domain = None
86-
if username is not None:
87-
if email and username.lower() == email.lower():
88-
# ignore the username if it's the same as the email (policy default)
89-
username = None
90-
elif id_type is not IdentityTypes.federatedID:
91-
raise ArgumentError("Username must match email except for Federated ID")
92-
else:
93-
if domain:
94-
self.domain = domain
95-
if email is not None:
96-
if '@' not in email:
97-
raise ArgumentError("Invalid email address: %s" % email)
98-
self.email = email
99-
if not self.domain:
100-
atpos = email.index('@')
101-
self.domain = email[atpos + 1:]
102-
elif not username:
103-
raise ArgumentError("No user identity specified.")
104-
elif not domain:
105-
raise ArgumentError("Both username and domain must be specified")
106-
107-
if id_type == IdentityTypes.adobeID:
108-
# by default if two users have the same email address, the UMAPI server will prefer the matching
109-
# Federated or Enterprise ID user; so we use the undocumented option to prefer the AdobeID match
110-
Action.__init__(self, user=email, useAdobeID=True, **kwargs)
111-
elif username and '@' in username:
112-
Action.__init__(self, user=username, **kwargs)
113-
elif username and '@' not in username:
114-
Action.__init__(self, user=username, domain=self.domain, **kwargs)
115-
else:
116-
Action.__init__(self, user=email, **kwargs)
77+
if '@' not in user and domain is None:
78+
raise ArgumentError("Domain required for non-email username")
79+
super().__init__(user=user, domain=domain, useAdobeID=use_adobe_id, **kwargs)
11780

11881
def __str__(self):
11982
return "UserAction "+str(self.__dict__)
12083

12184
def __repr__(self):
12285
return "UserAction "+str(self.__dict__)
12386

124-
def create(self, first_name=None, last_name=None, country=None, email=None,
87+
def create(self, email, first_name=None,
88+
last_name=None, country=None, id_type=IdentityTypes.federatedID,
12589
on_conflict=IfAlreadyExistsOptions.ignoreIfAlreadyExists):
12690
"""
12791
Create the user on the Adobe back end.
@@ -135,25 +99,22 @@ def create(self, first_name=None, last_name=None, country=None, email=None,
13599
:param on_conflict: IfAlreadyExistsOption (or string name thereof) controlling creation of existing users
136100
:return: the User, so you can do User(...).create(...).add_to_groups(...)
137101
"""
102+
if id_type in IdentityTypes.__members__:
103+
id_type = IdentityTypes[id_type]
104+
if id_type not in IdentityTypes:
105+
raise ArgumentError("Identity type (%s) must be one of %s" % (id_type, [i.name for i in IdentityTypes]))
138106
# first validate the params: email, on_conflict, first_name, last_name, country
139107
create_params = {}
140-
if email is None:
141-
if not self.email:
142-
raise ArgumentError("You must specify email when creating a user")
143-
elif self.email is None:
144-
self.email = email
145-
elif self.email.lower() != email.lower():
146-
raise ArgumentError("Specified email (%s) doesn't match user's email (%s)" % (email, self.email))
147108
create_params["email"] = self.email
148109
if on_conflict in IfAlreadyExistsOptions.__members__:
149110
on_conflict = IfAlreadyExistsOptions[on_conflict]
150111
if on_conflict not in IfAlreadyExistsOptions:
151112
raise ArgumentError("on_conflict must be one of {}".format([o.name for o in IfAlreadyExistsOptions]))
152113
if on_conflict != IfAlreadyExistsOptions.errorIfAlreadyExists:
153114
create_params["option"] = on_conflict.name
154-
if first_name: create_params["firstname"] = first_name # per issue #54 now allowed for all identity types
155-
if last_name: create_params["lastname"] = last_name # per issue #54 now allowed for all identity types
156-
if country: create_params["country"] = country # per issue #55 should not be defaulted
115+
if first_name: create_params["firstname"] = first_name
116+
if last_name: create_params["lastname"] = last_name
117+
if country: create_params["country"] = country
157118

158119
# each type is created using a different call
159120
if self.id_type == IdentityTypes.adobeID:

0 commit comments

Comments
 (0)