<commit_before>package models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.JsonView;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import utilities.ControllerHelper;

import javax.persistence.*;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@Entity
@Table(name="useraccount")
@JsonRootName("user")
@JsonIgnoreProperties(ignoreUnknown = true)
public class User extends Model {

    private static final long MIN_PASSWORD_LENGTH = 8;
    public final static Finder<Long, User> FIND = new Finder<Long, User>(Long.class, User.class);

    @JsonView({ControllerHelper.Summary.class})
    @Id
    private Long id;

    @Column(length = 256, nullable = false)
    @Constraints.Required
    @Constraints.MinLength(1)
    @Constraints.MaxLength(256)
    private String firstName;

    @Column(length = 256, nullable = false)
    @Constraints.Required
    @Constraints.MinLength(1)
    @Constraints.MaxLength(256)
    private String lastName;

    @Column(nullable = false, updatable = false)
    private Date creationDate;

    @JsonView({ControllerHelper.Summary.class})
    @Column(length = 256, unique = true, nullable = false)
    @Constraints.MaxLength(256)
    @Constraints.Required
    @Constraints.Email
    private String email;

    @Enumerated(EnumType.STRING)
    private Role role;

    @JsonIgnore
    @Column(length = 64, nullable = false)
    private byte[] shaPassword;

    // TODO: allow multiple browsers
    @JsonIgnore
    private String authToken;

    public User() {
        this.authToken = UUID.randomUUID().toString();
        role = Role.USER;
        this.creationDate = new Date();
    }

    public User(String email, String password, String firstName, String lastName) {
        this();
        setEmail(email);
        setPassword(password);
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public byte[] getShaPassword() {
        return shaPassword;
    }

    public void setPassword(String password) {
        shaPassword = getSha512(password);
    }

    public String getAuthToken() {

        // create a token if non exists for this user
        if(authToken == null) {
            authToken = UUID.randomUUID().toString();
            save();
        }

        return authToken;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email.toLowerCase();
    }

    public static byte[] getSha512(String value) {
        try {
            return MessageDigest.getInstance("SHA-512").digest(value.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            play.Logger.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            play.Logger.error(e.getMessage(), e);
        }
        return value.getBytes();
    }

    public boolean checkPassword(String password) {
        byte[] hash = getSha512(password);
        return Arrays.equals(shaPassword, hash);
    }

    public static User findByAuthToken(String authToken) {
        if (authToken == null)
            return null;

        try  {
            return FIND.where().eq("authToken", authToken).findUnique();
        } catch (Exception e) {
            play.Logger.error(e.getMessage(), e);
            return null;
        }
    }
<commit_msg>Added todo comment<commit_after>package models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.JsonView;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import utilities.ControllerHelper;

import javax.persistence.*;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@Entity
@Table(name="useraccount")
@JsonRootName("user")
@JsonIgnoreProperties(ignoreUnknown = true)
public class User extends Model {

    private static final long MIN_PASSWORD_LENGTH = 8;
    public final static Finder<Long, User> FIND = new Finder<Long, User>(Long.class, User.class);

    @JsonView({ControllerHelper.Summary.class})
    @Id
    private Long id;

    @Column(length = 256, nullable = false)
    @Constraints.Required
    @Constraints.MinLength(1)
    @Constraints.MaxLength(256)
    private String firstName;

    @Column(length = 256, nullable = false)
    @Constraints.Required
    @Constraints.MinLength(1)
    @Constraints.MaxLength(256)
    private String lastName;

    @Column(nullable = false, updatable = false)
    private Date creationDate;

    @JsonView({ControllerHelper.Summary.class})
    @Column(length = 256, unique = true, nullable = false)
    @Constraints.MaxLength(256)
    @Constraints.Required
    @Constraints.Email
    private String email;

    @Enumerated(EnumType.STRING)
    private Role role;

    @JsonIgnore
    @Column(length = 64, nullable = false)
    private byte[] shaPassword;

    // TODO: allow multiple browsers
    @JsonIgnore
    private String authToken;

    public User() {
        this.authToken = UUID.randomUUID().toString();
        role = Role.USER;
        this.creationDate = new Date();
    }

    public User(String email, String password, String firstName, String lastName) {
        this();
        setEmail(email);
        setPassword(password);
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public byte[] getShaPassword() {
        return shaPassword;
    }

    public void setPassword(String password) {
        shaPassword = getSha512(password);
    }

    public String getAuthToken() {

        // create a token if non exists for this user
        if(authToken == null) {
            authToken = UUID.randomUUID().toString();
            save();
        }

        return authToken;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email.toLowerCase();
    }

    public static byte[] getSha512(String value) {
        try {
            return MessageDigest.getInstance("SHA-512").digest(value.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            play.Logger.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            play.Logger.error(e.getMessage(), e);
        }
        // TODO: fix exception handling
        return value.getBytes();
    }

    public boolean checkPassword(String password) {
        byte[] hash = getSha512(password);
        return Arrays.equals(shaPassword, hash);
    }

    public static User findByAuthToken(String authToken) {
        if (authToken == null)
            return null;

        try  {
            return FIND.where().eq("authToken", authToken).findUnique();
        } catch (Exception e) {
            play.Logger.error(e.getMessage(), e);
            return null;
        }
    }

{'fraction_non_alphanumeric': 0.10284567245247372, 'fraction_numerical': 0.008501594048884165, 'mean_word_length': 2.6258561643835616, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1315170', 'n_tokens_mistral': 2465, 'n_tokens_neox': 2340, 'n_words': 572}