<commit_before>/* Copyright (c) 2011 Danish Maritime Authority.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.maritimecloud.portal.infrastructure.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.maritimecloud.portal.domain.model.identity.Role;
import net.maritimecloud.portal.domain.model.identity.UnknownUserException;
import net.maritimecloud.portal.domain.model.identity.User;
import net.maritimecloud.portal.domain.model.identity.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO: move back into test sources once a durable persistence mechanism has been introduced
 * 
 * In Memory implementation of the UserRepository for test purposes and as demo 
 * until a durable persistence mechanism is introduced.
 * 
 * @author <NAME>
 */
public class InMemoryUserRepository implements UserRepository, CleanableStore {

    private final Map<String, User> repository;

    private static final Logger LOG = LoggerFactory.getLogger(InMemoryUserRepository.class);

    public InMemoryUserRepository() {
        repository = new HashMap<>();
        initWithDummyUsers();
        LOG.warn("\n\n Using InMemoryUserRepository !!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"+repository);
    }

    private void initWithDummyUsers() {
        add(activate(new User("admin", "test", "<EMAIL>", Role.ADMIN, Role.USER)));
        add(activate(new User("Tintin", "test", "<EMAIL>")));
        add(activate(new User("Haddock", "test", "<EMAIL>")));
    }

    @Override
    public void add(User aUser) {
        String key = keyOf(aUser);
        if (repository().containsKey(key)) {
            throw new IllegalStateException("Duplicate key.");
        }
        repository().put(key, aUser);
    }

    @Override
    public void remove(User aUser) {
        String key = keyOf(aUser);
        repository().remove(key);
    }

    @Override
    public User userWithUsername(String aUsername) {
        for (User user : repository().values()) {
            if (user.username().equalsIgnoreCase(aUsername)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public List<User> usersWithUsernameMatching(String usernamePattern) {
        List<User> users = new ArrayList<>();
        repository().values().stream().filter((user) -> (user.username().matches(".*"+usernamePattern+".*"))).forEach((user) -> {
            users.add(user);
        });
        return users;
    }

    @Override
    public User userWithEmail(String emailAddress) {
        for (User user : repository().values()) {
            if (user.emailAddress().equalsIgnoreCase(emailAddress)) {
                return user;
            }
        }
        return null;
    }

//    @Override
//    public User userFromAuthenticCredentials(String aUsername, String anEncryptedPassword) {
//
//        for (User get : repository().values()) {
//            if (get.username().equals(aUsername)) {
//                if (get.internalAccessOnlyEncryptedPassword().equals(anEncryptedPassword)) {
//                    return get;
//                }
//            }
//        }
//        return null;
//    }
    
    @Override
    public void clean() {
        repository().clear();
    }

    private String keyOf(User aUser) {
        String key = aUser.username();
        return key;
    }

    private Map<String, User> repository() {
        return repository;
    }

    @Override
    public User get(long userId) throws UnknownUserException {
        for (User user : repository().values()) {
            if(user.id() == userId)
                return user;
        }
        throw new UnknownUserException(userId);
    }

    private User activate(User u) {
        u.generateActivationId();
        u.activate(u.activationId());
        return u;
    }

}
<commit_msg>Removed the User-role from the admin user (...it seems like it is shadowing the admin-role on the client)<commit_after>/* Copyright (c) 2011 Danish Maritime Authority.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.maritimecloud.portal.infrastructure.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.maritimecloud.portal.domain.model.identity.Role;
import net.maritimecloud.portal.domain.model.identity.UnknownUserException;
import net.maritimecloud.portal.domain.model.identity.User;
import net.maritimecloud.portal.domain.model.identity.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO: move back into test sources once a durable persistence mechanism has been introduced
 * 
 * In Memory implementation of the UserRepository for test purposes and as demo 
 * until a durable persistence mechanism is introduced.
 * 
 * @author <NAME>
 */
public class InMemoryUserRepository implements UserRepository, CleanableStore {

    private final Map<String, User> repository;

    private static final Logger LOG = LoggerFactory.getLogger(InMemoryUserRepository.class);

    public InMemoryUserRepository() {
        repository = new HashMap<>();
        initWithDummyUsers();
        LOG.warn("\n\n Using InMemoryUserRepository !!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"+repository);
    }

    private void initWithDummyUsers() {
        add(activate(new User("admin", "test", "<EMAIL>", Role.ADMIN/*, Role.USER*/)));
        add(activate(new User("Tintin", "test", "<EMAIL>")));
        add(activate(new User("Haddock", "test", "<EMAIL>")));
    }

    @Override
    public void add(User aUser) {
        String key = keyOf(aUser);
        if (repository().containsKey(key)) {
            throw new IllegalStateException("Duplicate key.");
        }
        repository().put(key, aUser);
    }

    @Override
    public void remove(User aUser) {
        String key = keyOf(aUser);
        repository().remove(key);
    }

    @Override
    public User userWithUsername(String aUsername) {
        for (User user : repository().values()) {
            if (user.username().equalsIgnoreCase(aUsername)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public List<User> usersWithUsernameMatching(String usernamePattern) {
        List<User> users = new ArrayList<>();
        repository().values().stream().filter((user) -> (user.username().matches(".*"+usernamePattern+".*"))).forEach((user) -> {
            users.add(user);
        });
        return users;
    }

    @Override
    public User userWithEmail(String emailAddress) {
        for (User user : repository().values()) {
            if (user.emailAddress().equalsIgnoreCase(emailAddress)) {
                return user;
            }
        }
        return null;
    }

//    @Override
//    public User userFromAuthenticCredentials(String aUsername, String anEncryptedPassword) {
//
//        for (User get : repository().values()) {
//            if (get.username().equals(aUsername)) {
//                if (get.internalAccessOnlyEncryptedPassword().equals(anEncryptedPassword)) {
//                    return get;
//                }
//            }
//        }
//        return null;
//    }
    
    @Override
    public void clean() {
        repository().clear();
    }

    private String keyOf(User aUser) {
        String key = aUser.username();
        return key;
    }

    private Map<String, User> repository() {
        return repository;
    }

    @Override
    public User get(long userId) throws UnknownUserException {
        for (User user : repository().values()) {
            if(user.id() == userId)
                return user;
        }
        throw new UnknownUserException(userId);
    }

    private User activate(User u) {
        u.generateActivationId();
        u.activate(u.activationId());
        return u;
    }

}

{'fraction_non_alphanumeric': 0.12051569506726457, 'fraction_numerical': 0.002242152466367713, 'mean_word_length': 2.6190669371196753, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '2282136', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2361, 'n_words': 657}