Skip to content
Snippets Groups Projects
right.py 3.16 KiB
Newer Older
Seblu's avatar
Seblu committed
#!/usr/bin/env python
#coding=utf8

'''
CloudControl right releated commands
'''

from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
from cccli.command.command import Command, TqlCommand
Seblu's avatar
Seblu committed

class Command_rights(TqlCommand):
Seblu's avatar
Seblu committed
    '''List account rights'''
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
        self.set_usage("%prog [options] [tql]")
        self.tql_filter += "&a&r=cli"
        self.remove_option("--direct")
        self.remove_option("--quiet")
        self.remove_option("--index")
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    def __call__(self, argv):
        # Parse argline
Seblu's avatar
Seblu committed
        # append current login if nothing asked
Seblu's avatar
Seblu committed
            tql = "a=%s"%self.cli.settings["login"]
        else:
Seblu's avatar
Seblu committed
        # ask server
        al = self.rpccall("rights", tql, _direct=True, _status=False)
Seblu's avatar
Seblu committed
        # display answer
        for (a, rl) in al.items():
            self.printer.out("%sa:%s%s%s"%(self.tdtc("a"), self.tdc("a"), a, color["reset"]))
Seblu's avatar
Seblu committed
            for i,r in enumerate(rl):
                tags = " ".join([ "%s%s:%s%s"%(self.tdtc(t), t, self.tdc(t), v) for (t,v) in r.items() ])
                self.printer.out("[%s] %s%s"%(i,tags, color["reset"]))
Seblu's avatar
Seblu committed

    def remote_functions(self):
        return set(('rights',))

Seblu's avatar
Seblu committed

class Command_addright(TqlCommand):
Seblu's avatar
Seblu committed
    '''Add or edit account right'''
Seblu's avatar
Seblu committed

    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
        self.tql_filter += "&a&r=cli"
        self.set_usage('''%prog [options] <account tql> <right tql> <method> <target> [index]

<method> is the name of the rpc command to allow
<target> can be allow or deny''')

Seblu's avatar
Seblu committed
    def __call__(self, argv):
        # argv check
        self.parse_args(argv)
        if len(self.args) == 4:
            self.args.append(None)
        elif len(self.args) == 5:
            self.args[4] = int(self.args[4])
        else:
Seblu's avatar
Seblu committed
            raise cmdBadArgument()
        # rpc call
        self.rpccall("addright",
                    self.args[0],
                    self.args[1],
                    self.args[2],
                    self.args[3],
                    self.args[4])
Seblu's avatar
Seblu committed

    def remote_functions(self):
        return set(('addright',))


class Command_delright(TqlCommand):
    '''Delete account right'''
Seblu's avatar
Seblu committed

    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
        self.tql_filter += "&a&r=cli"
        self.set_usage('''%prog [options] <tql> <index>
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed

    def __call__(self, argv):
        # argv check
        self.parse_args(argv)
        if len(self.args) != 2:
Seblu's avatar
Seblu committed
            raise cmdBadArgument()
Seblu's avatar
Seblu committed
            l = [ int(x) for x in self.args[1:] if x != "*" ]
        except ValueError as e:
            raise cmdBadArgument("Indexes must be numbers")
Seblu's avatar
Seblu committed
        if "*" in self.args[1:]:
            self.rpccall("delright", self.args[0], None)
                self.rpccall("delright", self.args[0], index)

    def remote_functions(self):
        return set(('delright',))