-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): adds command to manage user access in projects
- Loading branch information
1 parent
ca67437
commit 8721075
Showing
6 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2023 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import click | ||
from click_help_colors import HelpColorsGroup | ||
|
||
from riocli.constants import Colors | ||
from riocli.project.users.add import add_user_to_project | ||
from riocli.project.users.list import list_project_users | ||
from riocli.project.users.remove import remove_user_from_project | ||
|
||
|
||
@click.group( | ||
invoke_without_command=False, | ||
cls=HelpColorsGroup, | ||
help_headers_color=Colors.YELLOW, | ||
help_options_color=Colors.GREEN, | ||
) | ||
def users(): | ||
""" | ||
User access management on a project | ||
""" | ||
pass | ||
|
||
|
||
users.add_command(list_project_users) | ||
users.add_command(add_user_to_project) | ||
users.add_command(remove_user_from_project) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2023 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import click | ||
from click_help_colors import HelpColorsCommand | ||
from yaspin.api import Yaspin | ||
|
||
from riocli.config import new_client | ||
from riocli.constants import Colors, Symbols | ||
from riocli.project.users.util import get_user_guid_from_email | ||
from riocli.utils.spinner import with_spinner | ||
|
||
|
||
@click.command( | ||
'add', | ||
cls=HelpColorsCommand, | ||
help_headers_color=Colors.YELLOW, | ||
help_options_color=Colors.GREEN, | ||
) | ||
@click.argument('user-email', type=str) | ||
@click.option('--role', prompt_required=False, multiple=False, | ||
type=click.Choice(['admin', 'viewer']), | ||
default='admin', | ||
help='User role in the project') | ||
@click.pass_context | ||
@with_spinner(text="Adding user to project...") | ||
def add_user_to_project(ctx: click.Context, user_email: str, role: str, spinner: Yaspin = None) -> None: | ||
""" | ||
Add user to the project | ||
""" | ||
project_guid = ctx.obj.data.get('project_id') | ||
organization_guid = ctx.obj.data.get('organization_id') | ||
|
||
try: | ||
user_guid = get_user_guid_from_email(organization_guid, user_email) | ||
client = new_client(with_project=False) | ||
client.add_user_to_project(project_guid, user_guid, role) | ||
spinner.text = click.style('User added to the project', fg=Colors.GREEN) | ||
spinner.green.ok(Symbols.SUCCESS) | ||
except Exception as e: | ||
spinner.text = click.style('Failed to add user: {}'.format(e), fg=Colors.RED) | ||
spinner.red.ok(Symbols.ERROR) | ||
raise SystemExit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2023 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import typing | ||
|
||
import click | ||
from click_help_colors import HelpColorsCommand | ||
from munch import unmunchify | ||
|
||
from riocli.config import new_v2_client | ||
from riocli.constants import Colors | ||
from riocli.utils import tabulate_data | ||
|
||
|
||
@click.command( | ||
'list', | ||
cls=HelpColorsCommand, | ||
help_headers_color=Colors.YELLOW, | ||
help_options_color=Colors.GREEN, | ||
) | ||
@click.pass_context | ||
def list_project_users(ctx: click.Context) -> None: | ||
""" | ||
List users in the project | ||
""" | ||
try: | ||
client = new_v2_client(with_project=False) | ||
project = client.get_project(ctx.obj.data.get('project_id')) | ||
display_project_users(unmunchify(project.spec.users)) | ||
except Exception as e: | ||
click.secho(str(e), fg=Colors.RED) | ||
raise SystemExit(1) | ||
|
||
|
||
def display_project_users(users: typing.List[typing.Dict]) -> None: | ||
headers = ['ID', 'Name', 'Email', 'Status', 'Role'] | ||
data = [] | ||
for user in users: | ||
data.append([ | ||
user.get('userGUID'), | ||
'{} {}'.format(user.get('firstName'), user.get('lastName')), | ||
user.get('emailID'), | ||
user.get('userRole'), | ||
]) | ||
|
||
tabulate_data(data, headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2023 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import click | ||
from click_help_colors import HelpColorsCommand | ||
from yaspin.api import Yaspin | ||
|
||
from riocli.config import new_client | ||
from riocli.constants import Colors, Symbols | ||
from riocli.project.users.util import get_user_guid_from_email | ||
from riocli.utils.spinner import with_spinner | ||
|
||
|
||
@click.command( | ||
'remove', | ||
cls=HelpColorsCommand, | ||
help_headers_color=Colors.YELLOW, | ||
help_options_color=Colors.GREEN, | ||
) | ||
@click.argument('user-email', type=str) | ||
@click.pass_context | ||
@with_spinner(text="Removing user from project...") | ||
def remove_user_from_project(ctx: click.Context, user_email: str, spinner: Yaspin = None) -> None: | ||
""" | ||
Remove user from a project | ||
""" | ||
project_guid = ctx.obj.data.get('project_id') | ||
organization_guid = ctx.obj.data.get('organization_id') | ||
|
||
try: | ||
user_guid = get_user_guid_from_email(organization_guid, user_email) | ||
client = new_client(with_project=False) | ||
client.remove_user_from_project(project_guid, user_guid) | ||
spinner.text = click.style('User removed from the project', fg=Colors.GREEN) | ||
spinner.green.ok(Symbols.SUCCESS) | ||
except Exception as e: | ||
spinner.text = click.style('Failed to remove user: {}'.format(e), fg=Colors.RED) | ||
spinner.red.ok(Symbols.ERROR) | ||
raise SystemExit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2023 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import typing | ||
|
||
from riocli.organization.utils import get_organization_details | ||
|
||
|
||
def get_user_guid_from_email(organization_guid: str, user_email: str) -> typing.Any: | ||
organization = get_organization_details(organization_guid) | ||
users = organization.get('users') | ||
for user in users: | ||
if user['emailID'] == user_email: | ||
return user['guid'] | ||
|
||
return None |