2015-12-16 17:41:49 +00:00
|
|
|
import logging
|
2017-03-26 10:09:18 +00:00
|
|
|
from xattr import getxattr, removexattr, setxattr
|
2018-02-03 10:57:27 +00:00
|
|
|
from certidude import push
|
2016-03-27 20:38:14 +00:00
|
|
|
from certidude.decorators import serialize, csrf_protection
|
2018-02-03 11:10:45 +00:00
|
|
|
from .utils import AuthorityHandler
|
2018-05-02 08:11:01 +00:00
|
|
|
from .utils.firewall import login_required, authorize_admin
|
2015-12-16 17:41:49 +00:00
|
|
|
|
2017-04-04 05:02:08 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-12-16 17:41:49 +00:00
|
|
|
|
2018-02-03 11:10:45 +00:00
|
|
|
class TagResource(AuthorityHandler):
|
2015-12-16 17:41:49 +00:00
|
|
|
@serialize
|
|
|
|
@login_required
|
|
|
|
@authorize_admin
|
2017-03-26 00:10:09 +00:00
|
|
|
def on_get(self, req, resp, cn):
|
2018-02-03 10:57:27 +00:00
|
|
|
path, buf, cert, signed, expires = self.authority.get_signed(cn)
|
2017-03-26 10:09:18 +00:00
|
|
|
tags = []
|
|
|
|
try:
|
2017-12-30 13:57:48 +00:00
|
|
|
for tag in getxattr(path, "user.xdg.tags").decode("utf-8").split(","):
|
2017-03-26 10:09:18 +00:00
|
|
|
if "=" in tag:
|
|
|
|
k, v = tag.split("=", 1)
|
|
|
|
else:
|
|
|
|
k, v = "other", tag
|
|
|
|
tags.append(dict(id=tag, key=k, value=v))
|
|
|
|
except IOError: # No user.xdg.tags attribute
|
|
|
|
pass
|
|
|
|
return tags
|
|
|
|
|
2016-03-21 21:42:39 +00:00
|
|
|
|
2016-03-27 20:38:14 +00:00
|
|
|
@csrf_protection
|
2015-12-16 17:41:49 +00:00
|
|
|
@login_required
|
|
|
|
@authorize_admin
|
2017-03-26 00:10:09 +00:00
|
|
|
def on_post(self, req, resp, cn):
|
2018-02-03 10:57:27 +00:00
|
|
|
path, buf, cert, signed, expires = self.authority.get_signed(cn)
|
2017-03-26 00:10:09 +00:00
|
|
|
key, value = req.get_param("key", required=True), req.get_param("value", required=True)
|
2017-03-26 10:09:18 +00:00
|
|
|
try:
|
|
|
|
tags = set(getxattr(path, "user.xdg.tags").decode("utf-8").split(","))
|
|
|
|
except IOError:
|
|
|
|
tags = set()
|
|
|
|
if key == "other":
|
|
|
|
tags.add(value)
|
|
|
|
else:
|
|
|
|
tags.add("%s=%s" % (key,value))
|
|
|
|
setxattr(path, "user.xdg.tags", ",".join(tags).encode("utf-8"))
|
2018-05-04 08:54:55 +00:00
|
|
|
logger.info("Tag %s=%s set for %s by %s" % (key, value, cn, req.context.get("user")))
|
2017-03-26 00:10:09 +00:00
|
|
|
push.publish("tag-update", cn)
|
2015-12-16 17:41:49 +00:00
|
|
|
|
|
|
|
|
2017-03-26 00:10:09 +00:00
|
|
|
class TagDetailResource(object):
|
2018-02-03 10:57:27 +00:00
|
|
|
def __init__(self, authority):
|
|
|
|
self.authority = authority
|
|
|
|
|
2016-03-27 20:38:14 +00:00
|
|
|
@csrf_protection
|
2015-12-16 17:41:49 +00:00
|
|
|
@login_required
|
|
|
|
@authorize_admin
|
2017-03-26 10:09:18 +00:00
|
|
|
def on_put(self, req, resp, cn, tag):
|
2018-02-03 10:57:27 +00:00
|
|
|
path, buf, cert, signed, expires = self.authority.get_signed(cn)
|
2017-03-26 10:09:18 +00:00
|
|
|
value = req.get_param("value", required=True)
|
|
|
|
try:
|
|
|
|
tags = set(getxattr(path, "user.xdg.tags").decode("utf-8").split(","))
|
|
|
|
except IOError:
|
|
|
|
tags = set()
|
2017-04-13 15:42:38 +00:00
|
|
|
try:
|
|
|
|
tags.remove(tag)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-03-26 10:09:18 +00:00
|
|
|
if "=" in tag:
|
|
|
|
tags.add("%s=%s" % (tag.split("=")[0], value))
|
|
|
|
else:
|
|
|
|
tags.add(value)
|
|
|
|
setxattr(path, "user.xdg.tags", ",".join(tags).encode("utf-8"))
|
2018-05-04 08:54:55 +00:00
|
|
|
logger.info("Tag %s set to %s for %s by %s" % (tag, value, cn, req.context.get("user")))
|
2017-03-26 10:09:18 +00:00
|
|
|
push.publish("tag-update", cn)
|
|
|
|
|
|
|
|
@csrf_protection
|
|
|
|
@login_required
|
|
|
|
@authorize_admin
|
|
|
|
def on_delete(self, req, resp, cn, tag):
|
2018-02-03 10:57:27 +00:00
|
|
|
path, buf, cert, signed, expires = self.authority.get_signed(cn)
|
2017-12-30 13:57:48 +00:00
|
|
|
tags = set(getxattr(path, "user.xdg.tags").decode("utf-8").split(","))
|
2017-03-26 10:09:18 +00:00
|
|
|
tags.remove(tag)
|
|
|
|
if not tags:
|
|
|
|
removexattr(path, "user.xdg.tags")
|
|
|
|
else:
|
|
|
|
setxattr(path, "user.xdg.tags", ",".join(tags))
|
2018-05-04 08:54:55 +00:00
|
|
|
logger.info("Tag %s removed for %s by %s" % (tag, cn, req.context.get("user")))
|
2017-03-26 00:10:09 +00:00
|
|
|
push.publish("tag-update", cn)
|