Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
go
Commits
58f8286d
Commit
58f8286d
authored
Dec 29, 2018
by
David Haynes
🙆
Browse files
Add basic redirection back in
parent
b9c6baba
Pipeline
#3550
passed with stage
in 1 minute and 49 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
go/go_back/cas_callbacks.py
View file @
58f8286d
...
...
@@ -17,20 +17,21 @@ def pfparse(pf_name_result: str) -> list:
Parse what peoplefinder sends back to us and make a list out of it.
"""
# name comes in format of Anderson, Nicholas J
name_list
=
pf_name_result
.
split
(
','
)
name_list
=
pf_name_result
.
split
(
","
)
# there's random whitespace with the first name
first_name_section
=
name_list
[
1
].
strip
()
# check if there's a middle initial
mi_q
=
first_name_section
.
split
(
' '
)
mi_q
=
first_name_section
.
split
(
" "
)
# make sure that the additional elements aren't multiple names
if
len
(
mi_q
[
-
1
])
==
1
:
first_name
=
' '
.
join
(
mi_q
[:
-
1
])
first_name
=
" "
.
join
(
mi_q
[:
-
1
])
else
:
first_name
=
first_name_section
# our list containing the name of the person in a usable list
new_name_list
=
[
first_name
,
name_list
[
0
]]
return
new_name_list
def
pfinfo
(
uname
:
str
)
->
list
:
"""
Get information from peoplefinder.
...
...
@@ -43,36 +44,43 @@ def pfinfo(uname: str) -> list:
except
requests
.
exceptions
.
RequestException
as
ex
:
print
(
"Cannot resolve to peoplefinder api:"
,
ex
)
print
(
"Returning empty user info tuple."
)
return
[
''
,
''
]
return
[
""
,
""
]
else
:
pfjson
=
metadata
.
json
()
try
:
if
len
(
pfjson
[
'
results
'
])
==
1
:
if
pfjson
[
'
method
'
]
==
'
peoplefinder
'
:
name_str
=
pfjson
[
'
results
'
][
0
][
'
name
'
]
if
len
(
pfjson
[
"
results
"
])
==
1
:
if
pfjson
[
"
method
"
]
==
"
peoplefinder
"
:
name_str
=
pfjson
[
"
results
"
][
0
][
"
name
"
]
name
=
pfparse
(
name_str
)
elif
pfjson
[
'method'
]
==
'ldap'
:
name
=
[
pfjson
[
'results'
][
0
][
'givenname'
],
pfjson
[
'results'
][
0
][
'surname'
]]
elif
pfjson
[
"method"
]
==
"ldap"
:
name
=
[
pfjson
[
"results"
][
0
][
"givenname"
],
pfjson
[
"results"
][
0
][
"surname"
],
]
else
:
name
=
pfjson
[
'
results
'
][
0
][
'
name
'
]
name
=
pfjson
[
"
results
"
][
0
][
"
name
"
]
return
name
else
:
if
pfjson
[
'
method
'
]
==
'
peoplefinder
'
:
name_str
=
pfjson
[
'
results
'
][
1
][
'
name
'
]
if
pfjson
[
"
method
"
]
==
"
peoplefinder
"
:
name_str
=
pfjson
[
"
results
"
][
1
][
"
name
"
]
name
=
pfparse
(
name_str
)
elif
pfjson
[
'method'
]
==
'ldap'
:
name
=
[
pfjson
[
'results'
][
1
][
'givenname'
],
pfjson
[
'results'
][
1
][
'surname'
]]
elif
pfjson
[
"method"
]
==
"ldap"
:
name
=
[
pfjson
[
"results"
][
1
][
"givenname"
],
pfjson
[
"results"
][
1
][
"surname"
],
]
else
:
name
=
pfjson
[
'
results
'
][
0
][
'
name
'
]
name
=
pfjson
[
"
results
"
][
0
][
"
name
"
]
return
name
# if the name is not in peoplefinder, return empty first and last name
except
IndexError
as
ex
:
print
(
"Name not found in peoplefinder."
)
return
[
''
,
''
]
return
[
""
,
""
]
except
Exception
as
ex
:
print
(
"Unknown peoplefinder error:"
,
ex
)
print
(
"Returning empty user info tuple."
)
return
[
''
,
''
]
return
[
""
,
""
]
def
create_user
(
tree
:
list
):
"""
...
...
@@ -83,7 +91,8 @@ def create_user(tree: list):
try
:
username
=
tree
[
0
][
0
].
text
# error handling in pfinfo function
info_name
=
pfinfo
(
username
)
# info_name = pfinfo(username)
info_name
=
[
""
,
""
]
# set and save the user's email
email_str
=
"%s%s"
%
(
username
,
settings
.
EMAIL_DOMAIN
)
...
...
@@ -91,12 +100,12 @@ def create_user(tree: list):
username
=
username
,
email
=
email_str
,
first_name
=
info_name
[
0
],
last_name
=
info_name
[
1
]
last_name
=
info_name
[
1
]
,
)
# Password is a required User object field, though doesn't matter for our
# purposes because all user auth is handled through CAS, not Django's login.
user
.
set_password
(
'
cas_used_instead
'
)
if
os
.
environ
[
'
GO_ENV
'
]
!=
'
production
'
:
user
.
set_password
(
"
cas_used_instead
"
)
if
os
.
environ
[
"
GO_ENV
"
]
!=
"
production
"
:
user
.
is_staff
=
True
user
.
is_superuser
=
True
user
.
save
()
...
...
@@ -105,6 +114,5 @@ def create_user(tree: list):
print
(
"Created user object %s."
%
username
)
else
:
print
(
"User object already exists."
)
except
Exception
as
ex
:
print
(
"CAS callback unsuccessful:"
,
ex
)
go/go_back/urls.py
View file @
58f8286d
...
...
@@ -4,7 +4,7 @@ settings/urls.py
The URLs of the project and their associated view that requests are routed to.
"""
# Django Imports
from
django.urls
import
path
,
include
from
django.urls
import
path
,
include
,
re_path
from
django.contrib
import
admin
# Third Party
...
...
@@ -31,15 +31,17 @@ urlpatterns = [
path
(
"admin/"
,
admin
.
site
.
urls
,
name
=
"go_admin"
),
path
(
"auth/"
,
include
(
"rest_framework.urls"
)),
path
(
"auth/token/"
,
views
.
CustomAuthToken
.
as_view
()),
path
(
"auth/status/"
,
views
.
GetSessionInfo
.
as_view
())
#
# /view/<short> - View URL data. Cached for 15 minutes
path
(
"auth/status/"
,
views
.
GetSessionInfo
.
as_view
())
,
# /view/<short> - View URL data. Cached for 15 minutes
# re_path(r'^view/(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$',
# cache_page(60 * 15)(go.views.view), name='view'),
# re_path(r'^view/(?P<short>[-\w]+)$',
# cache_page(60 * 15)(go.views.view), name='view'),
# # Redirection regex.
# re_path(r'^(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$',
# go.views.redirection, name='redirection'),
# re_path(r'^(?P<short>[-\w]+)$',
# go.views.redirection, name='redirection'),
# Redirection regex.
re_path
(
r
"^(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$"
,
views
.
redirection
,
name
=
"redirection"
,
),
re_path
(
r
"^(?P<short>[-\w]+)$"
,
views
.
redirection
,
name
=
"redirection"
),
]
go/go_back/views.py
View file @
58f8286d
...
...
@@ -4,6 +4,7 @@ go/views.py
The functions that handle a request to a given URL. Get some data, manipulate
it, and return a rendered template.
"""
from
django.shortcuts
import
get_object_or_404
,
redirect
from
rest_framework
import
viewsets
,
permissions
from
rest_framework.authentication
import
TokenAuthentication
,
SessionAuthentication
from
rest_framework.views
import
APIView
...
...
@@ -72,3 +73,13 @@ class GetSessionInfo(APIView):
"token"
:
token
.
key
,
}
return
Response
(
session_info
)
def
redirection
(
request
,
short
):
"""
This view redirects a user based on the short URL they requested.
"""
# Get the URL object that relates to the requested Go link
url
=
get_object_or_404
(
URL
,
short__iexact
=
short
)
return
redirect
(
url
.
destination
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment