~ubuntu-branches/ubuntu/trusty/python-axiom/trusty

« back to all changes in this revision

Viewing changes to axiom/plugins/userbasecmd.py

  • Committer: Bazaar Package Importer
  • Author(s): Stefano Zacchiroli
  • Date: 2007-04-01 19:09:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070401190903-ar7yssg8lwv3si1h
Tags: upstream-0.5.0
ImportĀ upstreamĀ versionĀ 0.5.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from epsilon.hotfix import require
 
3
require('twisted', 'filepath_copyTo')
 
4
 
 
5
import getpass
 
6
 
 
7
from twisted.python import usage
 
8
from twisted.python import filepath
 
9
 
 
10
from axiom import attributes, userbase
 
11
from axiom.scripts import axiomatic
 
12
 
 
13
class UserbaseMixin:
 
14
    def installOn(self, other):
 
15
        # XXX check installation on other, not store
 
16
        for ls in self.store.query(userbase.LoginSystem):
 
17
            raise usage.UsageError("UserBase already installed")
 
18
        else:
 
19
            ls = userbase.LoginSystem(store=self.store)
 
20
            ls.installOn(other)
 
21
            return ls
 
22
 
 
23
class Install(axiomatic.AxiomaticSubCommand, UserbaseMixin):
 
24
    def postOptions(self):
 
25
        self.installOn(self.store)
 
26
 
 
27
class Create(axiomatic.AxiomaticSubCommand, UserbaseMixin):
 
28
    synopsis = "<username> <domain> [password]"
 
29
 
 
30
    def parseArgs(self, username, domain, password=None):
 
31
        self['username'] = self.decodeCommandLine(username)
 
32
        self['domain'] = self.decodeCommandLine(domain)
 
33
        self['password'] = password
 
34
 
 
35
    def postOptions(self):
 
36
        for ls in self.store.query(userbase.LoginSystem):
 
37
            break
 
38
        else:
 
39
            ls = self.installOn(self.store)
 
40
 
 
41
        msg = 'Enter new AXIOM password: '
 
42
        while not self['password']:
 
43
            password = getpass.getpass(msg)
 
44
            second = getpass.getpass('Repeat to verify: ')
 
45
            if password == second:
 
46
                self['password'] = password
 
47
            else:
 
48
                msg = 'Passwords do not match.  Enter new AXIOM password: '
 
49
 
 
50
        try:
 
51
            acc = ls.addAccount(self['username'],
 
52
                                self['domain'],
 
53
                                self['password'])
 
54
        except userbase.DuplicateUser:
 
55
            raise usage.UsageError("An account by that name already exists.")
 
56
 
 
57
 
 
58
class Disable(axiomatic.AxiomaticSubCommand):
 
59
    synopsis = "<username> <domain>"
 
60
 
 
61
    def parseArgs(self, username, domain):
 
62
        self['username'] = self.decodeCommandLine(username)
 
63
        self['domain'] = self.decodeCommandLine(domain)
 
64
 
 
65
    def postOptions(self):
 
66
        for acc in self.store.query(userbase.LoginAccount,
 
67
                                    attributes.AND(userbase.LoginAccount.username == self['username'],
 
68
                                                   userbase.LoginAccount.domain == self['domain'])):
 
69
            if acc.disabled:
 
70
                raise usage.UsageError("That account is already disabled.")
 
71
            else:
 
72
                acc.disabled = True
 
73
                break
 
74
        else:
 
75
            raise usage.UsageError("No account by that name exists.")
 
76
 
 
77
 
 
78
class List(axiomatic.AxiomaticSubCommand):
 
79
    def postOptions(self):
 
80
        acc = None
 
81
        for acc in self.store.query(userbase.LoginMethod):
 
82
            print acc.localpart + '@' + acc.domain,
 
83
            if acc.account.disabled:
 
84
                print '[DISABLED]'
 
85
            else:
 
86
                print
 
87
        if acc is None:
 
88
            print 'No accounts'
 
89
 
 
90
 
 
91
class UserBaseCommand(axiomatic.AxiomaticCommand):
 
92
    name = 'userbase'
 
93
    description = 'LoginSystem introspection and manipulation.'
 
94
 
 
95
    subCommands = [
 
96
        ('install', None, Install, "Install UserBase on an Axiom database"),
 
97
        ('create', None, Create, "Create a new user"),
 
98
        ('disable', None, Disable, "Disable an existing user"),
 
99
        ('list', None, List, "List users in an Axiom database"),
 
100
        ]
 
101
 
 
102
    def getStore(self):
 
103
        return self.parent.getStore()
 
104
 
 
105
 
 
106
class Extract(axiomatic.AxiomaticCommand):
 
107
    name = 'extract-user'
 
108
    description = 'Remove an account from the login system, moving its associated database to the filesystem.'
 
109
    optParameters = [
 
110
        ('address', 'a', None, 'localpart@domain-format identifier of the user store to extract.'),
 
111
        ('destination', 'd', None, 'Directory into which to extract the user store.')]
 
112
 
 
113
    def extractSubStore(self, localpart, domain, destinationPath):
 
114
        siteStore = self.parent.getStore()
 
115
        la = siteStore.findFirst(
 
116
            userbase.LoginMethod,
 
117
            attributes.AND(userbase.LoginMethod.localpart == localpart,
 
118
                           userbase.LoginMethod.domain == domain)).account
 
119
        userbase.extractUserStore(la, destinationPath)
 
120
 
 
121
    def postOptions(self):
 
122
        localpart, domain = self.decodeCommandLine(self['address']).split('@', 1)
 
123
        destinationPath = filepath.FilePath(
 
124
            self.decodeCommandLine(self['destination'])).child(localpart + '@' + domain + '.axiom')
 
125
        self.extractSubStore(localpart, domain, destinationPath)
 
126
 
 
127
class Insert(axiomatic.AxiomaticCommand):
 
128
    name = 'insert-user'
 
129
    description = 'Insert a user store, such as one extracted with "extract-user", into a site store and login system.'
 
130
    optParameters = [
 
131
        ('userstore', 'u', None, 'Path to user store to be inserted.')
 
132
        ]
 
133
 
 
134
    def postOptions(self):
 
135
        userbase.insertUserStore(self.parent.getStore(),
 
136
                                 filepath.FilePath(self.decodeCommandLine(self['userstore'])))