| 1 |
# -*- coding: iso8859-1 -*- |
|---|
| 2 |
# |
|---|
| 3 |
# Copyright (C) 2005 Matthew Good <trac@matt-good.net> |
|---|
| 4 |
# |
|---|
| 5 |
# "THE BEER-WARE LICENSE" (Revision 42): |
|---|
| 6 |
# <trac@matt-good.net> wrote this file. As long as you retain this notice you |
|---|
| 7 |
# can do whatever you want with this stuff. If we meet some day, and you think |
|---|
| 8 |
# this stuff is worth it, you can buy me a beer in return. Matthew Good |
|---|
| 9 |
# |
|---|
| 10 |
# Author: Matthew Good <trac@matt-good.net> |
|---|
| 11 |
|
|---|
| 12 |
from trac.core import * |
|---|
| 13 |
|
|---|
| 14 |
class IPasswordStore(Interface): |
|---|
| 15 |
"""An interface for Components that provide a storage method for users and |
|---|
| 16 |
passwords. |
|---|
| 17 |
""" |
|---|
| 18 |
|
|---|
| 19 |
def config_key(self): |
|---|
| 20 |
"""Returns a string used to identify this implementation in the config. |
|---|
| 21 |
This password storage implementation will be used if the value of |
|---|
| 22 |
the config property "account-manager.password_format" matches. |
|---|
| 23 |
""" |
|---|
| 24 |
|
|---|
| 25 |
def get_users(self): |
|---|
| 26 |
"""Returns an iterable of the known usernames |
|---|
| 27 |
""" |
|---|
| 28 |
|
|---|
| 29 |
def has_user(self, user): |
|---|
| 30 |
"""Returns whether the user account exists. |
|---|
| 31 |
""" |
|---|
| 32 |
|
|---|
| 33 |
def set_password(self, user, password): |
|---|
| 34 |
"""Sets the password for the user. This should create the user account |
|---|
| 35 |
if it doesn't already exist. |
|---|
| 36 |
Returns True if a new account was created, False if an existing account |
|---|
| 37 |
was updated. |
|---|
| 38 |
""" |
|---|
| 39 |
|
|---|
| 40 |
def check_password(self, user, password): |
|---|
| 41 |
"""Checks if the password is valid for the user. |
|---|
| 42 |
""" |
|---|
| 43 |
|
|---|
| 44 |
def delete_user(self, user): |
|---|
| 45 |
"""Deletes the user account. |
|---|
| 46 |
Returns True if the account existed and was deleted, False otherwise. |
|---|
| 47 |
""" |
|---|
| 48 |
|
|---|
| 49 |
class IAccountChangeListener(Interface): |
|---|
| 50 |
"""An interface for receiving account change events. |
|---|
| 51 |
""" |
|---|
| 52 |
|
|---|
| 53 |
def user_added(self, user, password): |
|---|
| 54 |
"""New user |
|---|
| 55 |
""" |
|---|
| 56 |
|
|---|
| 57 |
def user_password_changed(self, user, password): |
|---|
| 58 |
"""Password changed |
|---|
| 59 |
""" |
|---|
| 60 |
|
|---|
| 61 |
def user_deleted(self, user): |
|---|
| 62 |
"""User deleted |
|---|
| 63 |
""" |
|---|
| 64 |
|
|---|
| 65 |
class AccountManager(Component): |
|---|
| 66 |
"""The AccountManager component handles all user account management methods |
|---|
| 67 |
provided by the IPasswordStore interface. |
|---|
| 68 |
|
|---|
| 69 |
The methods will be handled by the underlying password storage |
|---|
| 70 |
implementation set in trac.ini with the "account-manager.password_format" |
|---|
| 71 |
setting. |
|---|
| 72 |
""" |
|---|
| 73 |
|
|---|
| 74 |
implements(IAccountChangeListener) |
|---|
| 75 |
|
|---|
| 76 |
stores = ExtensionPoint(IPasswordStore) |
|---|
| 77 |
change_listeners = ExtensionPoint(IAccountChangeListener) |
|---|
| 78 |
|
|---|
| 79 |
# Public API |
|---|
| 80 |
|
|---|
| 81 |
def get_users(self): |
|---|
| 82 |
return self.password_store.get_users() |
|---|
| 83 |
|
|---|
| 84 |
def has_user(self, user): |
|---|
| 85 |
return self.password_store.has_user(user) |
|---|
| 86 |
|
|---|
| 87 |
def set_password(self, user, password): |
|---|
| 88 |
if self.password_store.set_password(user, password): |
|---|
| 89 |
self._notify('created', user, password) |
|---|
| 90 |
else: |
|---|
| 91 |
self._notify('password_changed', user, password) |
|---|
| 92 |
|
|---|
| 93 |
def check_password(self, user, password): |
|---|
| 94 |
return self.password_store.check_password(user, password) |
|---|
| 95 |
|
|---|
| 96 |
def delete_user(self, user): |
|---|
| 97 |
if self.password_store.delete_user(user): |
|---|
| 98 |
self._notify('deleted', user) |
|---|
| 99 |
|
|---|
| 100 |
def password_store(self): |
|---|
| 101 |
fmt = self.config.get('account-manager', 'password_format') |
|---|
| 102 |
for store in self.stores: |
|---|
| 103 |
if store.config_key() == fmt: |
|---|
| 104 |
return store |
|---|
| 105 |
raise TracError('No password store found. Please configure ' |
|---|
| 106 |
'"account-manager.password_format" in trac.ini.') |
|---|
| 107 |
password_store = property(password_store) |
|---|
| 108 |
|
|---|
| 109 |
def _notify(self, func, *args): |
|---|
| 110 |
func = 'user_' + func |
|---|
| 111 |
for l in self.change_listeners: |
|---|
| 112 |
getattr(l, func)(*args) |
|---|
| 113 |
|
|---|
| 114 |
# IAccountChangeListener methods |
|---|
| 115 |
|
|---|
| 116 |
def user_created(self, user, password): |
|---|
| 117 |
self.log.info('Created new user: %s' % user) |
|---|
| 118 |
|
|---|
| 119 |
def user_password_changed(self, user, password): |
|---|
| 120 |
self.log.info('Updated password for user: %s' % user) |
|---|
| 121 |
|
|---|
| 122 |
def user_deleted(self, user): |
|---|
| 123 |
self.log.info('Deleted user: %s' % user) |
|---|