Skip to content

Instantly share code, notes, and snippets.

View yannbriancon's full-sized avatar

Yann Briançon yannbriancon

View GitHub Profile
@yannbriancon
yannbriancon / authentication_middleware.py
Created June 10, 2019 17:30
Django authentication middleware to automatically authenticate the users
from django.contrib import auth
from django.contrib.auth.middleware import MiddlewareMixin
from django.http import HttpResponseForbidden
class AutomaticUserLoginMiddleware(MiddlewareMixin):
def process_view(self, request, view_func, view_args, view_kwargs):
if not AutomaticUserLoginMiddleware._is_user_authenticated(request):
user = auth.authenticate(request)
@yannbriancon
yannbriancon / settings.py
Last active June 10, 2019 17:56
Configure a custom authentication backend to authenticate the users
...
# Application definition
AUTHENTICATION_BACKENDS = [
'AutomaticDjangoAuthentication.authentication_backend.AuthenticationBackend',
]
INSTALLED_APPS = [
...
@yannbriancon
yannbriancon / ldap.py
Created June 10, 2019 18:21
Check if a user can authenticate in a LDAP and get his information
from ldap3 import Server, Connection, ALL
LDAP_URL = 'ldap.forumsys.com'
# Check user authentication in the LDAP and return his information
def get_LDAP_user(username, password):
try:
server = Server(LDAP_URL, get_info=ALL)
connection = Connection(server,
'uid={username},dc=example,dc=com'.format(
@yannbriancon
yannbriancon / authentication_backend.py
Last active June 10, 2019 18:31
Update custom Django authentication backend to get the credentials from the query strings
import ...
class AuthenticationBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
# Get credentials from the query strings
username = request.GET.get('username')
password = request.GET.get('password')
@yannbriancon
yannbriancon / settings.py
Created June 10, 2019 18:50
Modify Django middleware settings to add a custom middleware
# Application definition
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
# Add the new middleware just after the default AuthenticationMiddleware that manages sessions and cookies
'AutomaticDjangoAuthentication.authentication_middleware.AutomaticUserLoginMiddleware',
@yannbriancon
yannbriancon / authentication_backend.py
Last active June 17, 2019 11:29
Django authentication backend to authenticate the users from a LDAP
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
from .services.ldap import get_LDAP_user
class AuthenticationBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
# Get the user information from the LDAP if he can be authenticated
@yannbriancon
yannbriancon / NotificationResourceIntTest.java
Created April 26, 2020 10:07
Integration test asserting the number of queries generated
...
import com.yannbriancon.interceptor.HibernateQueryCountInterceptor;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class NotificationResourceIntTest {
@Autowired
private HibernateQueryCountInterceptor hibernateQueryCountInterceptor;
@yannbriancon
yannbriancon / Message.java
Last active May 2, 2020 10:08
Message class
class Message {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private User author;
}
@yannbriancon
yannbriancon / MessageRepository.java
Created May 2, 2020 17:46
JPA query with entity graph
@EntityGraph(attributePaths = {"author"})
List<Message> getAllBy();
@yannbriancon
yannbriancon / MessageRepository.java
Created May 2, 2020 17:47
JPQL with JOIN FETCH
@Query("SELECT *
FROM Message m
LEFT JOIN FETCH m.author")
List<Message> getAllBy();