Skip to content

Commit a976e04

Browse files
committed
Made several changes to auth package. All Auth Services must now inherit from auth.base.AuthBase and have load, unload, and is_player_authorized methods implemented. Made AuthManager methods load_auth, unload_auth, and reload_auth all public instead of private.
1 parent 56ed7b8 commit a976e04

File tree

5 files changed

+98
-31
lines changed

5 files changed

+98
-31
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# ../auth/base.py
2+
3+
# =============================================================================
4+
# >> IMPORTS
5+
# =============================================================================
6+
# Source.Python Imports
7+
# Auth
8+
from auth import AuthLogger
9+
10+
11+
# =============================================================================
12+
# >> ALL DECLARATION
13+
# =============================================================================
14+
# Add all the global variables to __all__
15+
__all__ = [
16+
'AuthBase',
17+
]
18+
19+
20+
# =============================================================================
21+
# >> GLOBAL VARIABLES
22+
# =============================================================================
23+
# Get sp.auth.base logger
24+
AuthBaseLogger = AuthLogger.base
25+
26+
27+
# =============================================================================
28+
# >> CLASSES
29+
# =============================================================================
30+
class AuthBase(object):
31+
'''A basic Auth Service class to be inherited by other Auth services'''
32+
33+
def load(self):
34+
'''Base implementation of load that if called will print an error'''
35+
AuthBaseLogger.log_warning(
36+
'[SP Auth] ' + _auth_strings['Not Implemented'].get_string(
37+
classname=self.__class__.__name__, method='load'))
38+
39+
def unload(self):
40+
'''Base implementation of unload that if called will print an error'''
41+
AuthBaseLogger.log_warning(
42+
'[SP Auth] ' + _auth_strings['Not Implemented'].get_string(
43+
classname=self.__class__.__name__, method='unload'))
44+
45+
def is_player_authorized(self, *args):
46+
'''Base implementation of is_player_authorized
47+
that if called will print an error'''
48+
AuthBaseLogger.log_warning(
49+
'[SP Auth] ' + _auth_strings['Not Implemented'].get_string(
50+
classname=self.__class__.__name__, method='is_player_authorized'))

addons/source-python/packages/source-python/auth/commands.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def _load_auth_providers(providers):
138138
for provider in providers:
139139

140140
# Load the current provider
141-
AuthManager._load_auth(provider)
141+
AuthManager.load_auth(provider)
142142

143143

144144
def _unload_auth_providers(providers):
@@ -158,7 +158,7 @@ def _unload_auth_providers(providers):
158158
for provider in providers:
159159

160160
# Unload the current provider
161-
AuthManager._unload_auth(provider)
161+
AuthManager.unload_auth(provider)
162162

163163

164164
def _reload_auth_providers(providers=None):
@@ -174,7 +174,7 @@ def _reload_auth_providers(providers=None):
174174
for provider in providers:
175175

176176
# Reload the given provider
177-
AuthManager._reload_auth(provider)
177+
AuthManager.reload_auth(provider)
178178

179179

180180
def _print_auth_providers():

addons/source-python/packages/source-python/auth/manager.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Source.Python imports
77
# Auth
88
from auth import AuthLogger
9+
from auth.base import AuthBase
910
from auth.paths import AUTH_PROVIDER_PATH
1011
# Players
1112
from players.helpers import uniqueid_from_playerinfo
@@ -39,7 +40,7 @@ class _AuthManager(dict):
3940
'''Class used to store loaded auth providers
4041
and check if a player is authorized'''
4142

42-
def _load_auth(self, provider):
43+
def load_auth(self, provider):
4344
'''Loads the given provider'''
4445

4546
# Send a message that the auth provider is being loaded
@@ -69,19 +70,42 @@ def _load_auth(self, provider):
6970
# No need to go further
7071
return
7172

72-
# Attempt to load the provider
73-
self[provider] = __import__(
73+
# Import the provider's module
74+
module = __import__(
7475
'auth.providers.{0}'.format(provider), fromlist=[''])
7576

76-
# Call the provider's load function
77-
self[provider].load()
77+
# Loop through all objects in the module
78+
for module_object in dir(module):
79+
80+
# Get the object's instance
81+
instance = getattr(module, module_object)
82+
83+
# Is the current object a AuthBase instance?
84+
if isinstance(instance, AuthBase):
85+
86+
# Found the instance
87+
break
88+
89+
# Was no AuthBase instance found?
90+
else:
91+
92+
# Raise an error that the object was not found
93+
raise NotImplementedError(
94+
'No AuthBase instance found in provider'
95+
' "{0}"'.format(provider))
96+
97+
# Attempt to call the provider's load function
98+
instance.load()
99+
100+
# Add the provider to the dictionary
101+
self[provider] = instance
78102

79103
# Send a message that the provider was loaded
80104
AuthManagerLogger.log_message(
81105
'[SP Auth] ' + _auth_strings[
82106
'Load Successful'].get_string(provider=provider))
83107

84-
def _unload_auth(self, provider):
108+
def unload_auth(self, provider):
85109
'''Unloads the given provider'''
86110

87111
# Send a message that the auth provider is being unloaded
@@ -111,7 +135,7 @@ def _unload_auth(self, provider):
111135
'[SP Auth] ' + _auth_strings[
112136
'Unload Successful'].get_string(provider=provider))
113137

114-
def _reload_auth(self, provider):
138+
def reload_auth(self, provider):
115139
'''Reloads the given provider'''
116140

117141
# Unload the provider

addons/source-python/packages/source-python/auth/providers/simple.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# Source.Python Imports
1111
# Auth
12+
from auth.base import AuthBase
1213
from auth.paths import AUTH_CFG_PATH
1314

1415

@@ -29,9 +30,17 @@
2930
# =============================================================================
3031
# >> CLASSES
3132
# =============================================================================
32-
class _SimpleAuth(set):
33+
class _SimpleAuth(AuthBase, set):
3334
'''Class used to determine if a player is authorized'''
3435

36+
def load(self):
37+
'''Called when the service is loaded'''
38+
self._parse_admins()
39+
40+
def unload(self):
41+
'''Called when the service is unloaded'''
42+
self.clear()
43+
3544
def _parse_admins(self):
3645
'''
3746
Method used to get all uniqueids that are authorized on the server
@@ -65,23 +74,4 @@ def is_player_authorized(self, uniqueid, level, permission, flag):
6574
return False
6675

6776
# Get the _SimpleAuth instance
68-
_SimpleAuthInstance = _SimpleAuth()
69-
70-
is_player_authorized = _SimpleAuthInstance.is_player_authorized
71-
72-
73-
# =============================================================================
74-
# >> FUNCTIONS
75-
# =============================================================================
76-
def load():
77-
'''Loads the provider by getting all uniqueids that are authorized'''
78-
79-
# Parse the simple auth file
80-
_SimpleAuthInstance._parse_admins()
81-
82-
83-
def unload():
84-
'''Unloads the provider by clearing the set'''
85-
86-
# Clear the set
87-
_SimpleAuthInstance.clear()
77+
SimpleAuth = _SimpleAuth()

resource/source-python/translations/_core/auth_strings.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ fr = "Vous devez fournir le nom d'un fournisseur d'authentification lors de l'ut
6262
en = "Loaded Auth Providers:"
6363
de = "Geladene Auth Provider:"
6464
fr = "Fournisseurs d'authentification présentement chargés:"
65+
66+
[Not Implemented]
67+
en = "AuthService '$classname' does not have a registered method '$method'"

0 commit comments

Comments
 (0)