Conditions | 10 |
Total Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like AuthHandlerBase._get_username_for_request() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
43 | def _get_username_for_request(self, username, request): |
||
44 | impersonate_user = getattr(request, 'user', None) |
||
45 | |||
46 | if impersonate_user is not None: |
||
47 | # check this is a service account |
||
48 | try: |
||
49 | if not User.get_by_name(username).is_service: |
||
50 | message = "Current user is not a service and cannot " \ |
||
51 | "request impersonated tokens" |
||
52 | abort_request(status_code=http_client.BAD_REQUEST, |
||
53 | message=message) |
||
54 | return |
||
55 | username = impersonate_user |
||
56 | except (UserNotFoundError, StackStormDBObjectNotFoundError): |
||
57 | message = "Could not locate user %s" % \ |
||
58 | (impersonate_user) |
||
59 | abort_request(status_code=http_client.BAD_REQUEST, |
||
60 | message=message) |
||
61 | return |
||
62 | else: |
||
63 | impersonate_user = getattr(request, 'impersonate_user', None) |
||
64 | nickname_origin = getattr(request, 'nickname_origin', None) |
||
65 | if impersonate_user is not None: |
||
66 | try: |
||
67 | # check this is a service account |
||
68 | if not User.get_by_name(username).is_service: |
||
69 | raise NotServiceUserError() |
||
70 | username = User.get_by_nickname(impersonate_user, |
||
71 | nickname_origin).name |
||
72 | except NotServiceUserError: |
||
73 | message = "Current user is not a service and cannot " \ |
||
74 | "request impersonated tokens" |
||
75 | abort_request(status_code=http_client.BAD_REQUEST, |
||
76 | message=message) |
||
77 | return |
||
78 | except (UserNotFoundError, StackStormDBObjectNotFoundError): |
||
79 | message = "Could not locate user %s@%s" % \ |
||
80 | (impersonate_user, nickname_origin) |
||
81 | abort_request(status_code=http_client.BAD_REQUEST, |
||
82 | message=message) |
||
83 | return |
||
84 | except NoNicknameOriginProvidedError: |
||
85 | message = "Nickname origin is not provided for nickname '%s'" % \ |
||
86 | impersonate_user |
||
87 | abort_request(status_code=http_client.BAD_REQUEST, |
||
88 | message=message) |
||
89 | return |
||
90 | except AmbiguousUserError: |
||
91 | message = "%s@%s matched more than one username" % \ |
||
92 | (impersonate_user, nickname_origin) |
||
93 | abort_request(status_code=http_client.BAD_REQUEST, |
||
94 | message=message) |
||
95 | return |
||
96 | return username |
||
97 | |||
175 |