예제 #1
0
        public AuthenticationManager(IStorageManager storageManager)
        {
            _storageManager = storageManager;

            Guest = new BotGroup(GroupNames.Guest, 0);
            Registered = new BotGroup(GroupNames.Registered, 10);
            Administrator = new BotGroup(GroupNames.Administrator, 20);
            Owner = new BotGroup(GroupNames.Owner, 30);

            _groups.Add(Guest.Name, Guest);
            _groups.Add(Registered.Name, Registered);
            _groups.Add(Administrator.Name, Administrator);
            _groups.Add(Owner.Name, Owner);

            _userData = _storageManager.Global().GetOrCreate<Dictionary<String, UserData>>("Users");
            foreach(UserData data in _userData.Values)
            {
                if(!_groups.ContainsKey(data.GroupName))
                {
                    _logger.Error("User with username " + data.Username + " has invalid group name " + data.GroupName
                        + ". User was not added.");
                }
                else
                {
                    IBotUser user = new BotUser(data.Username, data.Password, _groups[data.GroupName], false);
                    _users.Add(data.Username, user);
                    _masks[user] = data.Masks;
                }
            }

            _allMasks = new HashSet<IdentityMask>(_masks.Values);
        }
예제 #2
0
        public IBotUser Register(IUser user, String username, String password)
        {
            if(IsIdentified(user))
                throw new InvalidOperationException("User is already identified.");

            if(_users.ContainsKey(username))
                throw new ArgumentException("Username is already registered.", "username");

            if(String.IsNullOrWhiteSpace(password))
                throw new ArgumentException("Password cannot be null, empty or whitespace.", "password");

            IBotUser botUser = new BotUser(username, password, Registered);
            _users.Add(username, botUser);
            _userData.Add(username, new UserData
            {
                Username = botUser.Username,
                Password = botUser.HashedPassword,
                GroupName = botUser.Group.Name
            });
            _identifiedUsers.Add(user, botUser);
            return botUser;
        }
예제 #3
0
        public IBotUser GetUser(IUser user)
        {
            if(!IsIdentifiedSkipMasks(user))
            {
                IBotUser maskUser = TryIdentityMask(user);
                if(maskUser != null)
                {
                    return maskUser;
                }

                if(!_guests.ContainsKey(user))
                {
                    IBotUser guest = new BotUser("Guest", String.Empty, Guest);
                    _guests.Add(user, guest);
                    return guest;
                }
                else
                {
                    return _guests[user];
                }
            }
            else
            {
                return _identifiedUsers[user];
            }
        }