Skip to content

Commit

Permalink
Modify credentials to sotre login within local json file
Browse files Browse the repository at this point in the history
  • Loading branch information
jlorrain authored and ClementHector committed Feb 7, 2022
1 parent 63223d3 commit 36ef85a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 52 deletions.
26 changes: 25 additions & 1 deletion openpype/modules/default_modules/shotgrid/lib/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import shotgun_api3
from shotgun_api3.shotgun import AuthenticationFault

from openpype.lib import OpenPypeSecureRegistry
from openpype.lib import OpenPypeSecureRegistry, OpenPypeSettingsRegistry
from openpype.modules.default_modules.shotgrid.lib.record import Credentials


Expand Down Expand Up @@ -32,6 +32,9 @@ def get_shotgrid_hostname(shotgrid_url: str) -> str:
return urlparse(valid_shotgrid_url).hostname


# Credentials storing function (using keyring)


def get_credentials(shotgrid_url: str) -> Optional[Credentials]:
hostname = get_shotgrid_hostname(shotgrid_url)
if not hostname:
Expand Down Expand Up @@ -80,6 +83,27 @@ def clear_credentials(shotgrid_url: str):
password_registry.delete_item(Credentials.password_key_prefix())


# Login storing function (using json)


def get_local_login() -> Optional[str]:
reg = OpenPypeSettingsRegistry()
try:
return str(reg.get_item("shotgrid_login"))
except Exception as e:
return None


def save_local_login(login: str):
reg = OpenPypeSettingsRegistry()
reg.set_item("shotgrid_login", login)


def clear_local_login():
reg = OpenPypeSettingsRegistry()
reg.delete_item("shotgrid_login")


def check_credentials(
login: str,
password: str,
Expand Down
4 changes: 2 additions & 2 deletions openpype/modules/default_modules/shotgrid/lib/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def get_shotgrid_settings() -> Dict[str, Any]:
return get_system_settings().get("modules", {}).get(MODULE_NAME, {})


def get_shotgrid_url() -> str:
return get_shotgrid_settings().get("shotgrid_url")
def get_shotgrid_servers() -> Dict[str, Any]:
return get_shotgrid_settings().get("shotgrid_settings", {})


def get_module_server_url() -> str:
Expand Down
16 changes: 0 additions & 16 deletions openpype/modules/default_modules/shotgrid/shotgrid_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ class ShotgridModule(
def initialize(self, modules_settings: Dict[str, Any]):
shotgrid_settings = modules_settings.get(self.name, dict())
self.enabled = shotgrid_settings.get("enabled", False)
shotgrid_url = shotgrid_settings.get("shotgrid_url").strip()

self.shotgrid_url = shotgrid_url

self.project_id = shotgrid_settings.get("project_id")

# if self.enabled and not self.project_id:
# raise Exception("Project id is not set in settings.")

def connect_with_modules(self, enabled_modules):
pass
Expand Down Expand Up @@ -64,11 +56,3 @@ def tray_exit(self, *args, **kwargs):

def tray_menu(self, tray_menu):
return self.tray_wrapper.tray_menu(tray_menu)

def create_shotgrid_session(self) -> shotgun_api3.Shotgun:
credentials_ = credentials.get_credentials(settings.get_shotgrid_url())
return shotgun_api3.Shotgun(
base_url=self.shotgrid_url,
login=credentials_.login,
password=credentials_.password,
)
50 changes: 25 additions & 25 deletions openpype/modules/default_modules/shotgrid/tray/credential_dialog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import Any
from Qt import QtCore, QtWidgets, QtGui

Expand All @@ -15,7 +16,7 @@ class CredentialsDialog(QtWidgets.QDialog):
url_label: QtWidgets.QLabel
login_label: QtWidgets.QLabel
password_label: QtWidgets.QLabel
url_input: QtWidgets.QLineEdit
url_input: QtWidgets.QComboBox
login_input: QtWidgets.QLineEdit
password_input: QtWidgets.QLineEdit
input_layout: QtWidgets.QFormLayout
Expand Down Expand Up @@ -45,12 +46,12 @@ def __init__(self, module, parent=None):
self.ui_init()

def ui_init(self):
self.url_label = QtWidgets.QLabel("Shotgrid URL:")
self.url_label = QtWidgets.QLabel("Shotgrid server:")
self.login_label = QtWidgets.QLabel("Login:")
self.password_label = QtWidgets.QLabel("Password:")

self.url_input = QtWidgets.QLineEdit()
self.url_input.setReadOnly(True)
self.url_input = QtWidgets.QComboBox()
# self.url_input.setReadOnly(True)

self.login_input = QtWidgets.QLineEdit()
self.login_input.setPlaceholderText("login")
Expand Down Expand Up @@ -92,19 +93,20 @@ def ui_init(self):
def show(self, *args, **kwargs):
super(CredentialsDialog, self).show(*args, **kwargs)
self._fill_shotgrid_url()
self._fill_shotgrid_login()
# self._fill_shotgrid_login()

def _fill_shotgrid_url(self):
url = settings.get_shotgrid_url()
servers = settings.get_shotgrid_servers()

if url:
self.url_input.setText(url)
if servers:
for k, v in servers.items():
self.url_input.addItem("{}".format(v.get('shotgrid_url')))
self._valid_input(self.url_input)
self.login_button.show()
self.logout_button.show()
enabled = True
else:
self.url_input.setText("Ask your admin to add the shotgrid url")
self.set_error("Ask your admin to add shotgrid server in settings")
self._invalid_input(self.url_input)
self.login_button.hide()
self.logout_button.hide()
Expand All @@ -114,14 +116,10 @@ def _fill_shotgrid_url(self):
self.password_input.setEnabled(enabled)

def _fill_shotgrid_login(self):
cred = credentials.get_credentials(settings.get_shotgrid_url())
login = cred.login
password = cred.password
login = credentials.clear_local_login()

if login:
self.login_input.setText(login)
if password:
self.password_input.setText(password)

def _clear_shotgrid_login(self):
self.login_input.setText("")
Expand All @@ -140,7 +138,7 @@ def _on_shotgrid_login_clicked(self):
missing.append("password")
self._invalid_input(self.password_input)

url = settings.get_shotgrid_url()
url = self.url_input.currentText()
if url == "":
missing.append("url")
self._invalid_input(self.url_input)
Expand All @@ -149,20 +147,22 @@ def _on_shotgrid_login_clicked(self):
self.set_error("You didn't enter {}".format(" and ".join(missing)))
return

if credentials.check_credentials(
login=login,
password=password,
shotgrid_url=url,
):
credentials.save_credentials(
login=login, password=password, shotgrid_url=url
)
self._on_login()
# if credentials.check_credentials(
# login=login,
# password=password,
# shotgrid_url=url,
# ):
credentials.save_local_login(
login=login
)
os.environ['OPENPYPE_SG_USER'] = login
self._on_login()

self.set_error("CANT LOGIN")

def _on_shotgrid_logout_clicked(self):
credentials.clear_credentials(settings.get_shotgrid_url())
credentials.clear_local_login()
del os.environ['OPENPYPE_SG_USER']
self._clear_shotgrid_login()
self._on_logout()

Expand Down
12 changes: 4 additions & 8 deletions openpype/modules/default_modules/shotgrid/tray/shotgrid_tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def show_batch_dialog(self):
width=785, height=480)
webview.start(gui='cef')


def show_connect_dialog(self):
self.show_credential_dialog()

Expand All @@ -63,14 +62,11 @@ def tray_menu(self, tray_menu):
m.addAction(shotgrid_manager_action)

def validate(self) -> bool:
shotgrid_url = settings.get_shotgrid_url()
login = credentials.get_local_login()

if not shotgrid_url:
if not login:
self.show_credential_dialog()
return True

cred = credentials.get_credentials(settings.get_shotgrid_url())
else:
os.environ['OPENPYPE_SG_USER'] = login

if cred.is_empty():
self.show_credential_dialog()
return True

0 comments on commit 36ef85a

Please sign in to comment.