def create_group(): """ Creates a new user group in Beaker. The request must be :mimetype:`application/json`. :jsonparam string group_name: Symbolic name for the group. :jsonparam string display_name: Human-friendly display name for the group. :jsonparam string description: Description of the group. :jsonparam string root_password: Optional root password for group jobs. If this is not set, group jobs will use the root password preferences of the job submitter. :jsonparam string membership_type: Specifies how group membership is populated. Possible values are: * normal: Group is initially empty, members are explicitly added and removed by group owner. * ldap: Membership is populated from the LDAP group with the same group name. * inverted: Group contains all Beaker users *except* users who have been explicitly excluded by the group owner. :status 201: The group was successfully created. """ user = identity.current.user data = read_json_request(request) if 'group_name' not in data: raise BadRequest400('Missing group_name key') if 'display_name' not in data: raise BadRequest400('Missing display_name key') # for backwards compatibility if data.pop('ldap', False): data['membership_type'] = 'ldap' try: Group.by_name(data['group_name']) except NoResultFound: pass else: raise Conflict409("Group '%s' already exists" % data['group_name']) with convert_internal_errors(): group = Group.lazy_create(group_name=data['group_name']) group.display_name = data['display_name'] group.description = data.get('description') group.root_password = data.get('root_password') session.add(group) group.record_activity(user=user, service=u'HTTP', field=u'Group', action=u'Created') if data.get('membership_type'): group.membership_type = GroupMembershipType.from_string( data['membership_type']) if group.membership_type == GroupMembershipType.ldap: group.refresh_ldap_members() else: # LDAP groups don't have any owners group.add_member(user, is_owner=True, agent=identity.current.user) response = jsonify(group.__json__()) response.status_code = 201 response.headers.add('Location', absolute_url(group.href)) return response
def update_group(group_name): """ Updates attributes of an existing group. The request body must be a JSON object containing one or more of the following keys. :jsonparam string group_name: New name for the group. :jsonparam string display_name: Display name of the group. :jsonparam string description: Description of the group. :jsonparam string root_password: Optional password. Can be an empty string. If empty, group jobs will use the root password preferences of the job submitter. :jsonparam string membership_type: New membership type for the group. See `POST /groups/` for more information. :status 200: Group was updated. :status 400: Invalid data was given. """ group = _get_group_by_name(group_name) if not group.can_edit(identity.current.user): raise Forbidden403('Cannot edit group') data = read_json_request(request) with convert_internal_errors(): user = identity.current.user renamed = False if 'group_name' in data: new_name = data['group_name'] if new_name != group.group_name: if Group.query.filter(Group.group_name == new_name).count(): raise Conflict409('Group %s already exists' % new_name) group.set_name(user, u'HTTP', new_name) renamed = True if 'display_name' in data: new_display_name = data['display_name'] if new_display_name != group.display_name: group.set_display_name(user, u'HTTP', new_display_name) if 'description' in data: new_description = data['description'] if new_description != group.description: group.set_description(user, u'HTTP', new_description) if 'root_password' in data: new_root_password = data['root_password'] if new_root_password != group.root_password: group.set_root_password(user, u'HTTP', new_root_password) # for backwards compatibility if data.pop('ldap', False): data['membership_type'] = 'ldap' if 'membership_type' in data: new_type = GroupMembershipType.from_string( data['membership_type']) if (new_type == GroupMembershipType.ldap and not group.can_edit_ldap(user)): raise BadRequest400('Cannot edit LDAP group %s' % group) if new_type != group.membership_type: group.membership_type = new_type response = jsonify(group.to_json()) if renamed: response.headers.add('Location', absolute_url(group.href)) return response