Skip to content

Commit

Permalink
Replace Change for Promote
Browse files Browse the repository at this point in the history
  • Loading branch information
amandahla committed Oct 9, 2023
1 parent ee8bc39 commit d8e32ff
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 37 deletions.
6 changes: 3 additions & 3 deletions actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ register-user:
default: false
required:
- username
change-user-admin:
promote-user-admin:
description: |
Change whether a user is a server administrator.
Promote a user as a server administrator.
You need to supply a user name.
properties:
username:
description: |
User name to be changed to admin.
User name to be promoted to admin.
type: string
12 changes: 6 additions & 6 deletions src-docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Helper module used to manage interactions with Synapse API.
- **SYNAPSE_PORT**
- **SYNAPSE_URL**
- **ADD_USER_ROOM_URL**
- **CHANGE_USER_ADMIN_URL**
- **PROMOTE_USER_ADMIN_URL**
- **CREATE_ROOM_URL**
- **DEACTIVATE_ACCOUNT_URL**
- **LIST_ROOMS_URL**
Expand Down Expand Up @@ -269,24 +269,24 @@ Make user a room's admin.

<a href="../src/synapse/api.py#L472"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `change_user_admin`
## <kbd>function</kbd> `promote_user_admin`

```python
change_user_admin(
promote_user_admin(
user: User,
server: Optional[str],
admin_access_token: Optional[str]
) → None
```

Change user to admin.
Promote user to admin.



**Args:**

- <b>`user`</b>: user to be changed to admin.
- <b>`server`</b>: to be used to create the user id.
- <b>`user`</b>: user to be promoted to admin.
- <b>`server`</b>: to be used to promote the user id.
- <b>`admin_access_token`</b>: server admin access token to be used.


Expand Down
4 changes: 2 additions & 2 deletions src-docs/charm.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Unit that this execution is responsible for.

---

<a href="../src/charm.py#L90"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm.py#L93"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `change_config`

Expand All @@ -81,7 +81,7 @@ Change configuration.

---

<a href="../src/charm.py#L80"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm.py#L83"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `replan_nginx`

Expand Down
68 changes: 68 additions & 0 deletions src-docs/promote_user_admin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!-- markdownlint-disable -->

<a href="../src/actions/promote_user_admin.py#L0"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

# <kbd>module</kbd> `promote_user_admin`
Module to interact with Promote User Admin action.


---

<a href="../src/actions/promote_user_admin.py#L34"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `promote_user_admin`

```python
promote_user_admin(
username: str,
server: Optional[str],
admin_access_token: Optional[str]
) → None
```

Run promote user admin action.



**Args:**

- <b>`username`</b>: username to be promoted.
- <b>`server`</b>: to be used to promote the user id.
- <b>`admin_access_token`</b>: server admin access token to call API.



**Raises:**

- <b>`PromoteUserAdminError`</b>: if something goes wrong while promoting the user to be an admin.


---

<a href="../src/actions/promote_user_admin.py#L17"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>class</kbd> `PromoteUserAdminError`
Exception raised when something fails while running promote-user-admin.

Attrs: msg (str): Explanation of the error.

<a href="../src/actions/promote_user_admin.py#L24"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>method</kbd> `__init__`

```python
__init__(msg: str)
```

Initialize a new instance of the PromoteUserAdminError exception.



**Args:**

- <b>`msg`</b> (str): Explanation of the error.





2 changes: 1 addition & 1 deletion src/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"""Actions package is used to run actions provided by the charm."""

# Exporting methods to be used for another modules
from .change_user_admin import ChangeUserAdminError, change_user_admin # noqa: F401
from .promote_user_admin import PromoteUserAdminError, promote_user_admin # noqa: F401
from .register_user import RegisterUserError, register_user # noqa: F401
from .reset_instance import ResetInstanceError, reset_instance # noqa: F401
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

"""Module to interact with Change User Admin action."""
"""Module to interact with Promote User Admin action."""

import logging
import typing
Expand All @@ -14,15 +14,15 @@
logger = logging.getLogger(__name__)


class ChangeUserAdminError(Exception):
"""Exception raised when something fails while running change-user-admin.
class PromoteUserAdminError(Exception):
"""Exception raised when something fails while running promote-user-admin.
Attrs:
msg (str): Explanation of the error.
"""

def __init__(self, msg: str):
"""Initialize a new instance of the ChangeUserAdminError exception.
"""Initialize a new instance of the PromoteUserAdminError exception.
Args:
msg (str): Explanation of the error.
Expand All @@ -31,22 +31,22 @@ def __init__(self, msg: str):


# admin_access_token is not a password
def change_user_admin(
def promote_user_admin(
username: str, server: typing.Optional[str], admin_access_token: typing.Optional[str]
) -> None:
"""Run register user action.
"""Run promote user admin action.
Args:
username: username to be changed.
server: to be used to create the user id.
username: username to be promoted.
server: to be used to promote the user id.
admin_access_token: server admin access token to call API.
Raises:
ChangeUserAdminError: if something goes wrong while changing the user to
PromoteUserAdminError: if something goes wrong while promoting the user to
be an admin.
"""
try:
user = User(username=username, admin=True)
synapse.change_user_admin(user=user, server=server, admin_access_token=admin_access_token)
synapse.promote_user_admin(user=user, server=server, admin_access_token=admin_access_token)
except synapse.APIError as exc:
raise ChangeUserAdminError(str(exc)) from exc
raise PromoteUserAdminError(str(exc)) from exc
17 changes: 10 additions & 7 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def __init__(self, *args: typing.Any) -> None:
self.framework.observe(self.on.reset_instance_action, self._on_reset_instance_action)
self.framework.observe(self.on.synapse_pebble_ready, self._on_pebble_ready)
self.framework.observe(self.on.register_user_action, self._on_register_user_action)
self.framework.observe(
self.on.promote_user_admin_action, self._on_promote_user_admin_action
)

def replan_nginx(self) -> None:
"""Replan NGINX."""
Expand Down Expand Up @@ -175,28 +178,28 @@ def _on_register_user_action(self, event: ActionEvent) -> None:
results = {"register-user": True, "user-password": user.password}
event.set_results(results)

def _on_change_user_admin_action(self, event: ActionEvent) -> None:
"""Change user admin and report action result.
def _on_promote_user_admin_action(self, event: ActionEvent) -> None:
"""Promote user admin and report action result.
Args:
event: Event triggering the reset instance action.
event: Event triggering the promote user admin action.
"""
results = {
"change-user-admin": False,
"promote-user-admin": False,
}
container = self.unit.get_container(synapse.SYNAPSE_CONTAINER_NAME)
if not container.can_connect():
event.fail("Failed to connect to container")
return
try:
admin_access_token = secret_storage.get_admin_access_token(self)
actions.change_user_admin(
actions.promote_user_admin(
username=event.params["username"],
server=self._charm_state.synapse_config.server_name,
admin_access_token=admin_access_token,
)
results["change-user-admin"] = True
except (PebbleServiceError, actions.ChangeUserAdminError) as exc:
results["promote-user-admin"] = True
except (PebbleServiceError, actions.PromoteUserAdminError) as exc:
self.model.unit.status = ops.BlockedStatus(str(exc))
event.fail(str(exc))
return
Expand Down
2 changes: 1 addition & 1 deletion src/synapse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
SYNAPSE_VERSION_REGEX,
VERSION_URL,
APIError,
change_user_admin,
create_management_room,
deactivate_user,
get_access_token,
get_room_id,
get_version,
make_room_admin,
override_rate_limit,
promote_user_admin,
register_user,
)
from .workload import ( # noqa: F401
Expand Down
12 changes: 6 additions & 6 deletions src/synapse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
SYNAPSE_URL = f"http://localhost:{SYNAPSE_PORT}"

ADD_USER_ROOM_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/join"
CHANGE_USER_ADMIN_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/users/user_id/admin"
PROMOTE_USER_ADMIN_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/users/user_id/admin"
CREATE_ROOM_URL = f"{SYNAPSE_URL}/_matrix/client/v3/createRoom"
DEACTIVATE_ACCOUNT_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/deactivate"
LIST_ROOMS_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/rooms"
Expand Down Expand Up @@ -469,16 +469,16 @@ def _do_request(
raise NetworkError(f"HTTP error from {url}.") from exc


def change_user_admin(
def promote_user_admin(
user: User,
server: typing.Optional[str],
admin_access_token: typing.Optional[str],
) -> None:
"""Change user to admin.
"""Promote user to admin.
Args:
user: user to be changed to admin.
server: to be used to create the user id.
user: user to be promoted to admin.
server: to be used to promote the user id.
admin_access_token: server admin access token to be used.
"""
authorization_token = f"Bearer {admin_access_token}"
Expand All @@ -487,5 +487,5 @@ def change_user_admin(
"admin": True,
}
user_id = f"@{user.username}:{server}"
url = CHANGE_USER_ADMIN_URL.replace("user_id", user_id)
url = PROMOTE_USER_ADMIN_URL.replace("user_id", user_id)
_do_request("POST", url, headers=headers, json=data)

0 comments on commit d8e32ff

Please sign in to comment.