Skip to content

Commit

Permalink
Add is_stuff attribute for creation user object
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 11, 2024
1 parent edebe07 commit e963a35
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 46 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Initialization requires 6 parameters, which are all str type:
| certificate | Yes | The public key for the Casdoor application's cert |
| org_name | Yes | Application.organization |
| application_name | Yes | Application.name |
| is_stuff | No | Save user attr 'is_stuff' if True |

```python
CASDOOR_CONFIG = {
Expand All @@ -51,7 +52,8 @@ CASDOOR_CONFIG = {
'client_secret': '<client-secret>',
'certificate': '''<certificate>''',
'org_name': 'built-in',
'application_name': 'app-built-in'
'application_name': 'app-built-in',
'is_stuff': False
}
```

Expand Down Expand Up @@ -85,4 +87,8 @@ To add a button for using the Casdoor login, for example:
<button><a href="{% url 'casdoor_sso' %}">casdoor</a></button>`
```


## Step4. application settings
You need select next token fields in the Casdoor:
- Name
- Email
- Password
5 changes: 2 additions & 3 deletions casdoor_auth/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from django.apps import AppConfig


class CasdoorAuth(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "casdoor_auth"
default_auto_field = "django.db.models.BigAutoField"
name = "casdoor_auth"
5 changes: 2 additions & 3 deletions casdoor_auth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
# limitations under the License.

from django.urls import path

from casdoor_auth import views

urlpatterns = [
path('login/', views.toLogin, name='casdoor_sso'),
path('callback/', views.callback, name='callback'),
path('login/', views.toLogin, name='casdoor_sso'),
path('callback/', views.callback, name='callback'),
]
17 changes: 6 additions & 11 deletions casdoor_auth/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,18 @@

from casdoor_auth.views import sdk


def get_users(self):
return sdk.get_users()

return sdk.get_users()

def get_user(request):
return sdk.get_user(request.GET.get('name'))

return sdk.get_user(request.GET.get('name'))

def add_user(request):
user = request.GET.get("name")
return sdk.add_user(user)

user = request.GET.get("name")
return sdk.add_user(user)

def update_user(request):
return sdk.add_user(request.GET.get('user'))

return sdk.add_user(request.GET.get('user'))

def delete_user(request):
return sdk.delete_user(request.GET.get('name'))
return sdk.delete_user(request.GET.get('name'))
56 changes: 29 additions & 27 deletions casdoor_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,39 @@
from django.contrib.auth import get_user_model
from django.shortcuts import redirect


User = get_user_model()

conf = settings.CASDOOR_CONFIG

sdk = CasdoorSDK(conf.get('endpoint'),
conf.get('client_id'),
conf.get('client_secret'),
conf.get('certificate'),
conf.get('org_name'),
conf.get('application_name'),
conf.get('endpoint'))

conf.get('client_id'),
conf.get('client_secret'),
conf.get('certificate'),
conf.get('org_name'),
conf.get('application_name'),
conf.get('endpoint'))

def toLogin(request):
redirect_url = sdk.get_auth_link(redirect_uri=settings.REDIRECT_URI)
return redirect(redirect_url)

redirect_url = sdk.get_auth_link(redirect_uri=settings.REDIRECT_URI)
return redirect(redirect_url)

def callback(request):
code = request.GET.get('code')
token = sdk.get_oauth_token(code)
if isinstance(token, dict) and 'access_token' in token:
token = token['access_token']
user = sdk.parse_jwt_token(token)
request.session['user'] = user
try:
in_user = User.objects.get(username=user.get('name'))
except:
in_user = User.objects.create_user(user.get('name'), user.get('email'), user.get('password'))
in_user.save()
in_user = authenticate(username=user.get('name'), password=user.get('password'))
login(request, in_user)
return redirect(settings.LOGIN_REDIRECT_URL)
code = request.GET.get('code')
token = sdk.get_oauth_token(code)
if isinstance(token, dict) and 'access_token' in token:
token = token['access_token']
user = sdk.parse_jwt_token(token)
request.session['user'] = user

try:
in_user = User.objects.get(username=user.get('name'))
except:
in_user = User.objects.create_user(user.get('name'),
user.get('email'),
user.get('password'))
if 'is_staff' in conf.keys():
in_user.is_staff = conf.get('is_staff')

in_user.save()

in_user = authenticate(username=user.get('name'), password=user.get('password'))
login(request, in_user)
return redirect(settings.LOGIN_REDIRECT_URL)

0 comments on commit e963a35

Please sign in to comment.