# HG changeset patch # User Sandro Knauß # Date 1348749042 -7200 # Node ID 047e4b4760aa428d74020478f03a894602062d27 # Parent 503ed1a61543321c282017d2df0341ef263593cf adding iroadduser diff -r 503ed1a61543 -r 047e4b4760aa bin/iro-adduser --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/iro-adduser Thu Sep 27 14:30:42 2012 +0200 @@ -0,0 +1,104 @@ +#!/usr/bin/env python2.7 + +# Copyright (c) 2012 netzguerilla.net +# +# This file is part of Iro. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to use, +# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the +# #Software, and to permit persons to whom the Software is furnished to do so, +# subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +from twisted.python import log + +from sqlalchemy import create_engine, pool +from sqlalchemy.exc import ArgumentError +import sys,os +import random + +import logging + +from iro.controller.pool import dbPool +from iro import config, __version__, install +from iro.model.schema import Offer, User, Userright +from iro.model.utils import WithSession + +observer = log.PythonLoggingObserver() +observer.start() + +logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s') + +import argparse + +parser = argparse.ArgumentParser(description='Iro adduser.') +parser.add_argument('-v', '--version', action='version', version="%s %s"%(os.path.basename(sys.argv[0]),__version__)) +parser.add_argument('name', help="username") +parser.add_argument('offers', nargs='+', help="offers for user") +args = parser.parse_args() + +if not install.checkConfig(): + logging.error("iro.conf is not in right format.") + logging.info("Please edit iro.conf") + sys.exit(1) + +config.init() + +try: + engine = create_engine(config.main.dburl, + poolclass = pool.SingletonThreadPool, pool_size=dbPool.maxthreads, ) +except ArgumentError: + logging.error("Can't connect to database") + logging.info("Please edit iro.conf") + sys.exit(1) + + +if not install.checkDatabaseConnection(): + logging.error("Can't connect to database") + logging.info("Please edit iro.conf") + sys.exit(1) + +if not install.checkDatabase(): + logging.error("Database not in right format.") + logging.info("Please edit iro.conf and run iro-install --install") + sys.exit(1) + + +#test offers +with WithSession(engine) as session: + available_offers = [i[0] for i in session.query(Offer.name)] + +error = False +for o in args.offers: + if o not in available_offers: + logging.error("Unknown offer: '%s'"%o) + error = True + +if error: + sys.exit(1) + +#create apikey +apikey = "".join([random.choice("0123456789abcdef") for i in range(15)]) + +#adduser to database +with WithSession(engine, False) as session: + u = User(name=args.name, apikey=apikey) + session.add(u) + for i,offer in enumerate(args.offers): + o = session.query(Offer).filter_by(name=offer).first() + u.rights.append(Userright(o,default=i+1)) + session.commit() +logging.info("created user %s:"% args.name) +logging.info("apikey = %s"% apikey) +logging.info("offers = %s"% ", ".join(args.offers))