How to check if a user belongs to a particular group

The method has_group​ in the model res.users​ is helpful for checking if a user belongs to a specific group.

For example, if we need to check if a user is a salesman in the system, the code should look like this: 

# In this example we will use the current active user
# but can be browsed and used any available user in the system
# Ex: self.env['res.users'].browse(<user_id>)
user = self.env.user

if user.has_group('sales_team.group_sale_salesman'): print('This user is a salesman')

Another method that is useful when need to check the current user's group membership is user_has_groups​. This method is available in all models and it returns True​ if the user has at least one of the provided groups in groups​ not preceded by !​ and  is not a member of any of the groups in groups preceded by !​. 

An example of this method that will confirm that the current active user is a salesman but not a purchase manager is the following:

if self.user_has_groups('sales_team.group_sale_salesman,!purchase.group_purchase_manager'):
print('This user salesman but not purchase manager.')