Files
freeipa/ipaserver/plugins/selfservice.py
T

216 lines
6.3 KiB
Python
Raw Normal View History

2010-12-08 13:33:40 -05:00
# Authors:
# Rob Crittenden <rcritten@redhat.com>
#
# Copyright (C) 2010 Red Hat
# see file 'COPYING' for use and warranty information
#
2010-12-09 13:59:11 +01:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2010-12-08 13:33:40 -05:00
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2010-12-09 13:59:11 +01:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-12-16 16:06:03 +01:00
from ipalib import _, ngettext
from ipalib import Str
from ipalib import api, crud, errors
from ipalib import output
2015-12-16 16:06:03 +01:00
from ipalib import Object
from ipalib.plugable import Registry
from .baseldap import gen_pkey_only_option, pkey_to_value
__doc__ = _("""
Self-service Permissions
2010-12-08 13:33:40 -05:00
A permission enables fine-grained delegation of permissions. Access Control
Rules, or instructions (ACIs), grant permission to permissions to perform
given tasks such as adding a user, modifying a group, etc.
A Self-service permission defines what an object can change in its own entry.
2010-12-08 13:33:40 -05:00
EXAMPLES:
Add a self-service rule to allow users to manage their address (using Bash
brace expansion):
ipa selfservice-add --permissions=write --attrs={street,postalCode,l,c,st} "Users manage their own address"
2010-12-08 13:33:40 -05:00
When managing the list of attributes you need to include all attributes
in the list, including existing ones.
Add telephoneNumber to the list (using Bash brace expansion):
ipa selfservice-mod --attrs={street,postalCode,l,c,st,telephoneNumber} "Users manage their own address"
2010-12-08 13:33:40 -05:00
Display our updated rule:
ipa selfservice-show "Users manage their own address"
2010-12-08 13:33:40 -05:00
Delete a rule:
ipa selfservice-del "Users manage their own address"
""")
2010-12-08 13:33:40 -05:00
register = Registry()
2011-01-21 09:20:01 +01:00
ACI_PREFIX=u"selfservice"
@register()
2010-12-08 13:33:40 -05:00
class selfservice(Object):
"""
Selfservice object.
"""
bindable = False
object_name = _('self service permission')
object_name_plural = _('self service permissions')
2010-10-25 19:55:57 -04:00
label = _('Self Service Permissions')
2011-07-13 21:10:47 -05:00
label_singular = _('Self Service Permission')
2010-12-08 13:33:40 -05:00
takes_params = (
Str('aciname',
cli_name='name',
label=_('Self-service name'),
doc=_('Self-service name'),
2010-12-08 13:33:40 -05:00
primary_key=True,
pattern='^[-_ a-zA-Z0-9]+$',
pattern_errmsg="May only contain letters, numbers, -, _, and space",
2010-12-08 13:33:40 -05:00
),
Str('permissions*',
2010-12-08 13:33:40 -05:00
cli_name='permissions',
label=_('Permissions'),
doc=_('Permissions to grant (read, write). Default is write.'),
2010-12-08 13:33:40 -05:00
),
Str('attrs+',
2010-12-08 13:33:40 -05:00
cli_name='attrs',
label=_('Attributes'),
doc=_('Attributes to which the permission applies.'),
normalizer=lambda value: value.lower(),
2010-12-08 13:33:40 -05:00
),
2016-06-30 06:37:16 +02:00
Str('aci',
label=_('ACI'),
flags={'no_create', 'no_update', 'no_search'},
),
2010-12-08 13:33:40 -05:00
)
def __json__(self):
json_friendly_attributes = (
2011-06-23 19:48:50 -05:00
'label', 'label_singular', 'takes_params', 'bindable', 'name',
'object_name', 'object_name_plural',
)
json_dict = dict(
(a, getattr(self, a)) for a in json_friendly_attributes
)
2010-10-25 19:55:57 -04:00
json_dict['primary_key'] = self.primary_key.name
2019-10-18 22:56:47 +03:00
json_dict['methods'] = list(self.methods)
return json_dict
2012-02-02 21:28:15 +01:00
def postprocess_result(self, result):
try:
# do not include prefix in result
del result['aciprefix']
except KeyError:
pass
2010-12-08 13:33:40 -05:00
@register()
2010-12-08 13:33:40 -05:00
class selfservice_add(crud.Create):
__doc__ = _('Add a new self-service permission.')
2010-12-08 13:33:40 -05:00
msg_summary = _('Added selfservice "%(value)s"')
def execute(self, aciname, **kw):
2016-06-03 10:05:34 +02:00
if 'permissions' not in kw:
2010-12-08 13:33:40 -05:00
kw['permissions'] = (u'write',)
kw['selfaci'] = True
2011-01-21 09:20:01 +01:00
kw['aciprefix'] = ACI_PREFIX
2010-12-08 13:33:40 -05:00
result = api.Command['aci_add'](aciname, **kw)['result']
2012-02-02 21:28:15 +01:00
self.obj.postprocess_result(result)
2010-12-08 13:33:40 -05:00
return dict(
result=result,
value=pkey_to_value(aciname, kw),
2010-12-08 13:33:40 -05:00
)
@register()
2010-12-08 13:33:40 -05:00
class selfservice_del(crud.Delete):
__doc__ = _('Delete a self-service permission.')
2010-12-08 13:33:40 -05:00
has_output = output.standard_boolean
2010-12-08 13:33:40 -05:00
msg_summary = _('Deleted selfservice "%(value)s"')
def execute(self, aciname, **kw):
result = api.Command['aci_del'](aciname, aciprefix=ACI_PREFIX)
2012-02-02 21:28:15 +01:00
self.obj.postprocess_result(result)
2011-01-21 09:20:01 +01:00
2010-12-08 13:33:40 -05:00
return dict(
result=True,
value=pkey_to_value(aciname, kw),
2010-12-08 13:33:40 -05:00
)
@register()
2010-12-08 13:33:40 -05:00
class selfservice_mod(crud.Update):
__doc__ = _('Modify a self-service permission.')
2010-12-08 13:33:40 -05:00
msg_summary = _('Modified selfservice "%(value)s"')
def execute(self, aciname, **kw):
if 'attrs' in kw and kw['attrs'] is None:
raise errors.RequirementError(name='attrs')
2011-01-21 09:20:01 +01:00
kw['aciprefix'] = ACI_PREFIX
2010-12-08 13:33:40 -05:00
result = api.Command['aci_mod'](aciname, **kw)['result']
2012-02-02 21:28:15 +01:00
self.obj.postprocess_result(result)
2010-12-08 13:33:40 -05:00
return dict(
result=result,
value=pkey_to_value(aciname, kw),
2010-12-08 13:33:40 -05:00
)
@register()
2010-12-08 13:33:40 -05:00
class selfservice_find(crud.Search):
__doc__ = _('Search for a self-service permission.')
2010-12-08 13:33:40 -05:00
msg_summary = ngettext(
2011-02-23 16:47:49 -05:00
'%(count)d selfservice matched', '%(count)d selfservices matched', 0
2010-12-08 13:33:40 -05:00
)
takes_options = (gen_pkey_only_option("name"),)
def execute(self, term=None, **kw):
2010-12-08 13:33:40 -05:00
kw['selfaci'] = True
2011-01-21 09:20:01 +01:00
kw['aciprefix'] = ACI_PREFIX
2010-12-08 13:33:40 -05:00
result = api.Command['aci_find'](term, **kw)['result']
2011-01-21 09:20:01 +01:00
for aci in result:
2012-02-02 21:28:15 +01:00
self.obj.postprocess_result(aci)
2011-01-21 09:20:01 +01:00
2010-12-08 13:33:40 -05:00
return dict(
result=result,
count=len(result),
truncated=False,
)
@register()
2010-12-08 13:33:40 -05:00
class selfservice_show(crud.Retrieve):
__doc__ = _('Display information about a self-service permission.')
2010-12-08 13:33:40 -05:00
def execute(self, aciname, **kw):
2012-02-02 21:28:15 +01:00
result = api.Command['aci_show'](aciname, aciprefix=ACI_PREFIX, **kw)['result']
self.obj.postprocess_result(result)
2010-12-08 13:33:40 -05:00
return dict(
result=result,
value=pkey_to_value(aciname, kw),
2010-12-08 13:33:40 -05:00
)