|
24 | 24 |
|
25 | 25 | import nav.errors
|
26 | 26 | from nav.config import NAVConfigParser
|
| 27 | +from nav.models.profiles import Account, AccountGroup |
27 | 28 |
|
28 | 29 | _logger = logging.getLogger(__name__)
|
29 | 30 |
|
@@ -121,11 +122,66 @@ def open_ldap():
|
121 | 122 | return lconn
|
122 | 123 |
|
123 | 124 |
|
124 |
| -def authenticate(login, password): |
| 125 | +def authenticate(username, password): |
125 | 126 | """
|
126 |
| - Attempt to authenticate the login name with password against the |
127 |
| - configured LDAP server. If the user is authenticated, required |
128 |
| - group memberships are also verified. |
| 127 | + Authenticate the username and password against the configured LDAP server. |
| 128 | +
|
| 129 | + Required group memberships are also verified. |
| 130 | +
|
| 131 | + Returns an authenticated Account with updated groups, or None. |
| 132 | + """ |
| 133 | + if not available: |
| 134 | + return None |
| 135 | + ldap_user = get_ldap_user(username, password) |
| 136 | + try: |
| 137 | + account = Account.objects.get(login__iexact=username, ext_sync='ldap') |
| 138 | + except Account.DoesNotExist: |
| 139 | + if ldap_user: |
| 140 | + account = autocreate_ldap_user(ldap_user, password) |
| 141 | + return account |
| 142 | + if account.locked: |
| 143 | + _logger.info("Locked user %s tried to log in", account.login) |
| 144 | + return None |
| 145 | + if account.check_password(password): |
| 146 | + account = update_ldap_user(ldap_user, account, password) |
| 147 | + return account |
| 148 | + return None |
| 149 | + |
| 150 | + |
| 151 | +def autocreate_ldap_user(ldap_user, password): |
| 152 | + account = Account( |
| 153 | + login=ldap_user.username, |
| 154 | + name=ldap_user.get_real_name(), |
| 155 | + ext_sync='ldap', |
| 156 | + ) |
| 157 | + account = update_ldap_user(ldap_user, account, password) |
| 158 | + return account |
| 159 | + |
| 160 | + |
| 161 | +def update_ldap_user(ldap_user, account, password): |
| 162 | + account.set_password(password) |
| 163 | + account.save() |
| 164 | + _handle_ldap_admin_status(ldap_user, account) |
| 165 | + return account |
| 166 | + |
| 167 | + |
| 168 | +def _handle_ldap_admin_status(ldap_user, nav_account): |
| 169 | + is_admin = ldap_user.is_admin() |
| 170 | + # Only modify admin status if an entitlement is configured in webfront.conf |
| 171 | + if is_admin is not None: |
| 172 | + admin_group = AccountGroup.objects.get(id=AccountGroup.ADMIN_GROUP) |
| 173 | + if is_admin: |
| 174 | + nav_account.groups.add(admin_group) |
| 175 | + else: |
| 176 | + nav_account.groups.remove(admin_group) |
| 177 | + |
| 178 | + |
| 179 | +def get_ldap_user(login, password): |
| 180 | + """ |
| 181 | + Fetch an LDAPUser from an ldap server if login and password matches. |
| 182 | +
|
| 183 | + Returns an autenticated LDAPUser of a specific group or with specific |
| 184 | + entitlements, or False. |
129 | 185 | """
|
130 | 186 | lconn = open_ldap()
|
131 | 187 | server = _config.get('ldap', 'server')
|
|
0 commit comments