aboutsummaryrefslogtreecommitdiff
path: root/fietsboek/scripts/fietsctl.py
blob: 75c615a842761164c9dd467e6a32d65768ab8c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""Script to do maintenance work on a Fietsboek instance."""
# pylint: disable=consider-using-f-string,unused-argument
import argparse
import getpass
import sys

from pyramid.paster import bootstrap, setup_logging
from sqlalchemy import select

from .. import models, __VERSION__


EXIT_OKAY = 0
EXIT_FAILURE = 1


def cmd_useradd(env, args):
    """Create a new user.

    This user creation bypasses the "enable_account_registration" setting. It
    also immediately sets the new user's account to being verified.

    If email, name or password are not given as command line arguments, they
    will be asked for interactively.

    On success, the created user's unique ID will be printed.

    Note that this function does less input validation and should therefore be used with care!
    """
    email = args.email
    if not email:
        email = input("Email address: ")
    name = args.name
    if not name:
        name = input("Name: ")
    password = args.password
    if not password:
        password = getpass.getpass()

    # The UNIQUE constraint only prevents identical emails from being inserted,
    # but does not take into account the case insensitivity. The least we
    # should do here to not brick log ins for the user is to check if the email
    # already exists.
    query = models.User.query_by_email(email)
    with env["request"].tm:
        result = env["request"].dbsession.execute(query).scalar_one_or_none()
        if result is not None:
            print("Error: The given email already exists!", file=sys.stderr)
            return EXIT_FAILURE

    user = models.User(name=name, email=email, is_verified=True, is_admin=args.admin)
    user.set_password(password)

    with env["request"].tm:
        dbsession = env["request"].dbsession
        dbsession.add(user)
        dbsession.flush()
        user_id = user.id

    print(user_id)
    return EXIT_OKAY


def cmd_userdel(env, args):
    """Delete a user.

    This command deletes the user's account as well as any tracks associated
    with it.

    This command is destructive and irreversibly deletes data.
    """
    if args.id:
        query = select(models.User).filter_by(id=args.id)
    else:
        query = models.User.query_by_email(args.email)
    with env["request"].tm:
        dbsession = env["request"].dbsession
        user = dbsession.execute(query).scalar_one_or_none()
        if user is None:
            print("Error: No such user found.", file=sys.stderr)
            return EXIT_FAILURE
        print(user.name)
        print(user.email)
        if not args.force:
            query = input("Really delete this user? [y/N] ")
            if query not in {"Y", "y"}:
                print("Aborted by user.")
                return EXIT_FAILURE
        dbsession.delete(user)
        print("User deleted")
        return EXIT_OKAY


def cmd_userlist(env, args):
    """Prints a listing of all user accounts.

    The format is
        [av] {ID} - {email} - {Name}
    one line per user. The 'a' is added for admin accounts, the 'v' is added
    for verified users.
    """
    with env["request"].tm:
        dbsession = env["request"].dbsession
        users = dbsession.execute(select(models.User).order_by(models.User.id)).scalars()
        for user in users:
            tag = "[{}{}]".format(
                "a" if user.is_admin else "-",
                "v" if user.is_verified else "-",
            )
            print(f"{tag} {user.id} - {user.email} - {user.name}")
    return EXIT_OKAY


def cmd_passwd(env, args):
    """Change the password of a user."""
    if args.id:
        query = select(models.User).filter_by(id=args.id)
    else:
        query = models.User.query_by_email(args.email)
    with env["request"].tm:
        dbsession = env["request"].dbsession
        user = dbsession.execute(query).scalar_one_or_none()
        if user is None:
            print("Error: No such user found.", file=sys.stderr)
            return EXIT_FAILURE
        password = args.password
        if not password:
            password = getpass.getpass()
            repeat = getpass.getpass("Repeat password: ")
            if password != repeat:
                print("Error: Mismatched passwords.")
                return EXIT_FAILURE

        user.set_password(password)
        print(f"Changed password of {user.name} ({user.email})")
        return EXIT_OKAY


def cmd_version():
    """Show the installed fietsboek version."""
    name = __name__.split(".", 1)[0]
    print(f"{name} {__VERSION__}")


def parse_args(argv):
    """Parse the given args.

    :param argv: List of arguments.
    :type argv: list[str]
    :return: The parsed arguments.
    :rtype: argparse.Namespace
    """
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "-c",
        "--config",
        dest="config_uri",
        help="configuration file, e.g., development.ini",
    )

    subparsers = parser.add_subparsers(help="available subcommands", required=True)

    p_version = subparsers.add_parser(
        "version",
        help="show the version",
        description=cmd_version.__doc__,
    )
    p_version.set_defaults(func=cmd_version)

    p_useradd = subparsers.add_parser(
        "useradd",
        help="create a new user",
        description=cmd_useradd.__doc__,
    )
    p_useradd.add_argument(
        "--email",
        help="email address of the user",
    )
    p_useradd.add_argument(
        "--name",
        help="name of the user",
    )
    p_useradd.add_argument(
        "--password",
        help="password of the user",
    )
    p_useradd.add_argument(
        "--admin",
        action="store_true",
        help="make the new user an admin",
    )
    p_useradd.set_defaults(func=cmd_useradd)

    p_userdel = subparsers.add_parser(
        "userdel",
        help="delete a user account",
        description=cmd_userdel.__doc__,
    )
    p_userdel.add_argument(
        "--force",
        "-f",
        action="store_true",
        help="override the safety check",
    )
    group = p_userdel.add_mutually_exclusive_group(required=True)
    group.add_argument(
        "--id",
        "-i",
        type=int,
        help="database ID of the user",
    )
    group.add_argument(
        "--email",
        "-e",
        help="email of the user",
    )
    p_userdel.set_defaults(func=cmd_userdel)

    p_userlist = subparsers.add_parser(
        "userlist",
        help="list user accounts",
        description=cmd_userlist.__doc__,
    )
    p_userlist.set_defaults(func=cmd_userlist)

    p_passwd = subparsers.add_parser(
        "passwd",
        help="change user password",
        description=cmd_userdel.__doc__,
    )
    p_passwd.add_argument(
        "--password",
        help="password of the user",
    )
    group = p_passwd.add_mutually_exclusive_group(required=True)
    group.add_argument(
        "--id",
        "-i",
        type=int,
        help="database ID of the user",
    )
    group.add_argument(
        "--email",
        "-e",
        help="email of the user",
    )
    p_passwd.set_defaults(func=cmd_passwd)

    return parser.parse_args(argv[1:]), parser


def main(argv=None):
    """Main entry point."""
    if argv is None:
        argv = sys.argv
    args, parser = parse_args(argv)

    if args.func == cmd_version:  # pylint: disable=comparison-with-callable
        cmd_version()
        sys.exit(EXIT_OKAY)

    if not args.config_uri:
        parser.error("the following arguments are required: -c/--config")

    setup_logging(args.config_uri)
    env = bootstrap(args.config_uri)

    sys.exit(args.func(env, args))