aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/gmg_commands/users.py
blob: 368834d0be7c86cd0eb95c44b0432e89b26c95d1 (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
import os

from mediagoblin.gmg_commands import util as commands_util
from mediagoblin.db.open import setup_connection_and_db_from_config
from mediagoblin.auth import lib as auth_lib
from mediagoblin import mg_globals

def adduser_usage():
    print '\nUsage: adduser -u username -p password -m email'


def adduser_parser_setup(subparser):
    subparser.add_argument(
        '-u', '--username',
        help="Username used to login")
    subparser.add_argument(
        '-p', '--password',
        help="Your supersecret word to login")
    subparser.add_argument(
        '-m', '--email',
        help="Email to recieve notifications")
    subparser.add_argument(
        '-cf', '--conf_file', default='mediagoblin.ini',
        help="Config file used to set up environment")


def adduser(args):
    #TODO: Lets trust admins this do not validate Emails :)
    if args.username == None :
        print 'You must provide an username'
        adduser_usage()
    elif args.password == None:
        print 'You must provide a password'
        adduser_usage()
    elif args.email == None:
        print 'You must provide an email'
        adduser_usage()
    else:
        mgoblin_app = commands_util.setup_app(args)

        db = mg_globals.database
        users_with_username = \
            db.User.find({
                'username': args.username.lower()
            }).count()

        if users_with_username:
            print u'Sorry, a user with that name already exists.'

        else:
            # Create the user
            entry = db.User()
            entry['username'] = unicode(args.username.lower())
            entry['email'] = unicode(args.email)
            entry['pw_hash'] = auth_lib.bcrypt_gen_password_hash(args.password)
            entry['email_verified'] = True
            entry.save(validate=True)

            print "User Created: Already Verified :)"


def makeadmin_usage():
    print '\nUsage: makeadmin -u username'


def makeadmin_parser_setup(subparser):
    subparser.add_argument(
        '-u', '--username',
        help="Username to give admin level")
    subparser.add_argument(
        '-cf', '--conf_file', default='mediagoblin.ini',
        help="Config file used to set up environment")


def makeadmin(args):
    if args.username == None:
        print 'You must provide an username'
        makeadmin_usage()
    else:
        mgoblin_app = commands_util.setup_app(args)

        db = mg_globals.database

        user = db.User.one({'username':unicode(args.username.lower())})
        if user:
            user['is_admin'] = True
            user.save()
            print 'The user is now Admin'
        else:
            print 'The user doesn\'t exist'


def changepw_usage():
    print '\nUsage: changepw -u username -p new_password'


def changepw_parser_setup(subparser):
    subparser.add_argument(
        '-u', '--username',
        help="Username used to login")
    subparser.add_argument(
        '-p', '--password',
        help="Your NEW supersecret word to login")
    subparser.add_argument(
        '-cf', '--conf_file', default='mediagoblin.ini',
        help="Config file used to set up environment")


def changepw(args):
    if args.username == None:
        print 'You must provide an username'
        adduser_usage()
    elif args.password == None:
        print 'You must provide a password'
        adduser_usage()
    else:
        mgoblin_app = commands_util.setup_app(args)

        db = mg_globals.database

        user = db.User.one({'username':unicode(args.username.lower())})
        if user:
            user['pw_hash'] = auth_lib.bcrypt_gen_password_hash(args.password)
            user.save()
            print 'Password successfully changed'
        else:
            print 'The user doesn\'t exist'