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 / 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 / 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 / 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 / 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 / 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 / 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 / NotificationService.java
Last active May 4, 2020 19:59
Service triggering N+1 queries
void logMessages() {
// Get all the messages from the database
// -> Triggers 1 query
Set<Message> messages = messageDao.findAll();
// Map through the N messages to create the DTO with the author display name
// -> Triggers 1 query to fetch each author so N queries!
messages.stream.map(
message -> logger.info(
message.getAuthor().getName() + ": " + message.getText()
@yannbriancon
yannbriancon / pom.xml
Last active May 10, 2020 13:42
spring-hibernate-query-count dependency
<dependency>
<groupId>com.yannbriancon</groupId>
<artifactId>spring-hibernate-query-utils</artifactId>
<version>1.0.3</version>
</dependency>
@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;