# Path: nappingcat/exceptions.py
# class NappingCatRejected(NappingCatException):
#     pass
# 
# Path: nappingcat/response.py
# def Success(content, response_type=Response):
#     return response_type(200, content)
# 
# Path: nappingcat/decorators.py
# def discoverable(params, help_text):
#     def inner(func):
#         return DiscoverableEndpoint(func.__name__, params, help_text, func)
#     return inner 


from nappingcat.exceptions import NappingCatRejected
from nappingcat.response import Success 
from nappingcat.decorators import discoverable
import sys
import os

PERMISSION_SEP = '::'

@discoverable({'username':'string'},"""
add_user <username>
 
    create a new user.
""".strip())
def add_user(request, username):
    auth = request.auth_backend
    if auth.has_permission(request.user, ('auth', 'adduser')):
        auth.add_user(username)
        return Success({'message':"Added user '%s'" % username})
    raise NappingCatRejected("You don't have permission to add a user.")

@discoverable({'username':'string', 'key':'stdin'},"""
add_key_to_user <username>
 
    add a key (read from stdin) to an existing user.
""".strip())
def add_key_to_user(request, username):
    auth = request.auth_backend
    if auth.has_permission(request.user, ('auth', 'modifyuser')):
        key = request.stdin.read()
        settings = dict(request.settings.items('kittyconfig'))
        auth.add_key_to_user(username, key) 
        return Success({'message':"You added the key '%s...' to the user '%s'" % (key[0:30], username)})

# Path: nappingcat/exceptions.py
# class NappingCatRejected(NappingCatException):
#     pass
# 
# Path: nappingcat/response.py
# def Success(content, response_type=Response):
#     return response_type(200, content)
# 
# Path: nappingcat/decorators.py
# def discoverable(params, help_text):
#     def inner(func):
#         return DiscoverableEndpoint(func.__name__, params, help_text, func)
#     return inner 


# Path: nappingcat/contrib/auth/handlers.py
from nappingcat.exceptions import NappingCatRejected
from nappingcat.response import Success 
from nappingcat.decorators import discoverable
import sys
import os


PERMISSION_SEP = '::'

@discoverable({'username':'string'},"""
add_user <username>
 
    create a new user.
""".strip())
def add_user(request, username):
    auth = request.auth_backend
    if auth.has_permission(request.user, ('auth', 'adduser')):
        auth.add_user(username)
        return Success({'message':"Added user '%s'" % username})
    raise NappingCatRejected("You don't have permission to add a user.")

@discoverable({'username':'string', 'key':'stdin'},"""
add_key_to_user <username>
 
    add a key (read from stdin) to an existing user.
""".strip())
def add_key_to_user(request, username):
    auth = request.auth_backend
    if auth.has_permission(request.user, ('auth', 'modifyuser')):
        key = request.stdin.read()
        settings = dict(request.settings.items('kittyconfig'))
        auth.add_key_to_user(username, key) 
        return Success({'message':"You added the key '%s...' to the user '%s'" % (key[0:30], username)})

    raise NappingCatRejected("You don't have permission to modify users.")