def update(obj, first_name, last_name, email, username, password, user): """ Updates an existing user. """ # Create the updated user object attrs = { "first_name": first_name, "last_name": last_name, "email_address": email, "username": username, "password": password } # Get rid of 'None' values attrs = {key: value for (key, value) in attrs.items() if value is not None} # If there are no fields to update exit with an error if len(attrs) is 0: raise click.UsageError(message="You must specify a field to update.") output.debug("Updating '%s' with '%s'" % (user["username"], attrs)) # Finally update the user updated = obj.api.update_user(user["id"], attrs) output.info(render_user(updated))
def create(state, bundle, config_file, layer): """Create a new dynamic configuration layer for a bundle. Layers are combined to create the final dynamic configuration that is in place when a command is executed. The layer names are as follows: - "base": in the absence of anything else, this configuration will be in effect. - "room/$ROOM": commands executed in the chat room `$ROOM` will layer this configuration on top of `base` - "user/$USER": commands executed by the user `$USER` will layer this configuration on last If no `--layer` option is given, a `base` layer is created. The configuration file given must be a YAML map. For more details, see https://cog-book.operable.io/sections/dynamic_command_configuration.html. """ try: with click.open_file(config_file) as f: content = yaml.load(f.read()) except yaml.parser.ParserError: raise click.BadParameter("Invalid YAML", param_hint=["config_file"]) layer, name = layer r = state.api.create_config_layer(bundle, layer, name, content) output.info("Created {} layer for '{}' bundle".format( layer_name(r), r['bundle_name']))
def password_reset(obj, token, password): """ Reset user password with a token. A token is received by email after a password reset request. """ output.info("Resetting password") obj.api.password_reset(token, password)
def delete(state, bundle, layer): """ Delete a dynamic configuration layer. """ state.api.delete_dynamic_config(bundle, layer['layer'], layer['name']) output.info("Deleted '{}' layer for bundle '{}'".format( layer_name(layer), bundle['name']))
def delete(state, groups): """Delete relay groups. Groups must have neither bundles assigned to it, nor relays associated with it. """ for g in groups: state.api.delete_relay_group(g) output.info("Deleted relay group '{}'".format(g["name"]))
def layers(state, bundle): """List the configuration layers of a bundle. """ layer_names = sorted( [layer_name(c) for c in state.api.dynamic_configs(bundle)]) if layer_names: for l in layer_names: click.echo(l) else: output.info("No dynamic configuration layers for {}".format( bundle['name']))
def create(obj, first_name, last_name, email, username, password): """ Create a new user. """ user = obj.api.new_user({ "first_name": first_name, "last_name": last_name, "email_address": email, "username": username, "password": password }) output.info(render_user(user))
def create(state, name, relays): """Create a relay group. If any relay names are supplied, they will be added as members of the newly-created group. """ group = state.api.create_relay_group(name) output.info("Created relay group '{}'".format(group["name"])) if relays: add_relays_to_group(state.api, group, relays)
def show_current_relay_members(response): """Given an API response object, generate a message showing the current relay members of a group. Use after modifying the membership. """ if response["relays"]: msg = "Relay group '{}' has the following relay members: {}".format( response["name"], ", ".join(sorted([r["name"] for r in response["relays"]]))) else: msg = "Relay group '{}' has no relay members.".format(response["name"]) output.info(msg)
def show_current_assigned_bundles(response): """Given an API response object, generate a message showing the currently assigned bundles of a group. Use after modifying the membership. """ if response["bundles"]: msg = "Relay group '{}' has the following assigned bundles: {}".format( response["name"], ", ".join(sorted([b["name"] for b in response["bundles"]]))) else: msg = "Relay group '{}' has no assigned bundles.".format( response["name"]) output.info(msg)
def create(obj, name): """ Create a new user group. """ group = obj.api.new_group(name) output.info(group_table(group))
def revoke(obj, group, roles): """ Revoke one or more roles from a group. """ group = obj.api.revoke_group_roles(group['id'], roles) output.info(group_table(group))
def grant(obj, group, roles): """ Grant one or more roles to a group. """ group = obj.api.grant_group_roles(group['id'], roles) output.info(group_table(group))
def remove(obj, group, usernames): """ Remove one or more users from a group. """ group = obj.api.remove_group_users(group['id'], usernames) output.info(group_table(group))
def add(obj, group, usernames): """ Add one or more users to a group. """ group = obj.api.add_group_users(group['id'], usernames) output.info(group_table(group))
def delete(obj, group): """ Delete a group. """ obj.api.delete_group(group['id']) output.info(group_table(group))
def password_reset_request(obj, email): """ Request a password reset. """ output.info("Requesting password reset") obj.api.request_password_reset(email)
def rename(obj, group, new_name): """ Rename a user group. """ group = obj.api.update_group(group['id'], {'name': new_name}) output.info(group_table(group))
def delete(obj, user): """ Deletes a user. """ obj.api.delete_user(user['id']) output.info(render_user(user))