Exemplo n.º 1
0
        public override void Add(int accountId, IUser user)
        {
            if (string.IsNullOrEmpty(user.Name))
            {
                throw new UsersException(ErrorCodes.UsernameEmpty);
            }

            var sync = GetSync(accountId);

            sync.EnterWriteLock();
            try
            {
                var users = accounts.GetOrAdd(accountId, NewAccount);

                if (users.ContainsKey(user.Name))
                {
                    throw new UsersException(ErrorCodes.UserExist);
                }

                users.Add(user.Name, new CsvUser(user));

                var fileName = files.GetFileName(accountId);

                if (Directory.Exists(Path.GetDirectoryName(fileName)) == false)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                }

                File.AppendAllText(fileName, "\r\n" + CsvUser.ToString(user));
            }
            finally
            {
                sync.ExitWriteLock();
            }
        }
Exemplo n.º 2
0
        private void WriteUsers(int accountId, Dictionary <string, CsvUser> users)
        {
            files.IgnoryFileChanges(accountId);
            using (var file = File.CreateText(files.GetFileName(accountId)))
            {
                files.IgnoryFileChanges(accountId);
                file.WriteLine("# " + CsvUser.GetFormatDescription());

                foreach (var user in users)
                {
                    files.IgnoryFileChanges(accountId);
                    file.WriteLine(user.Value.ToString());
                }
            }
        }
Exemplo n.º 3
0
        private void ReadUsers(int accountId, string fileName)
        {
            var sync = GetSync(accountId);

            sync.EnterWriteLock();
            try
            {
                var users = NewAccount(accountId);
                accounts.Replace(accountId, users);

                using (var csv = new CsvReader(new StreamReader(fileName), false))
                {
                    csv.SkipEmptyLines          = true;
                    csv.MissingFieldAction      = MissingFieldAction.ReplaceByEmpty;
                    csv.DefaultParseErrorAction = ParseErrorAction.AdvanceToNextLine;

                    int      fieldCount = csv.FieldCount;
                    string[] headers    = csv.GetFieldHeaders();

                    while (csv.ReadNextRecord())
                    {
                        var user = new CsvUser();

                        if (user.Read(csv))
                        {
                            if (users.ContainsKey(user.Name) == false)
                            {
                                users.Add(user.Name, user);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                // должно вызываться снаружи лока
                //OnError(ex.Message);
            }
            finally
            {
                sync.ExitWriteLock();
            }

            OnReset(accountId);
        }
Exemplo n.º 4
0
        public override IUser GetByName(int accountId, string username)
        {
            var sync = GetSync(accountId);

            sync.EnterReadLock();
            try
            {
                CsvUser user = null;

                var users = accounts.GetValue(accountId);
                if (users != null)
                {
                    users.TryGetValue(username, out user);
                }

                return(user);
            }
            finally
            {
                sync.ExitReadLock();
            }
        }
Exemplo n.º 5
0
		private void ReadUsers(int accountId, string fileName)
		{
			var sync = GetSync(accountId);
			sync.EnterWriteLock();
			try
			{
				var users = NewAccount(accountId);
				accounts.Replace(accountId, users);

				using (var csv = new CsvReader(new StreamReader(fileName), false))
				{
					csv.SkipEmptyLines = true;
					csv.MissingFieldAction = MissingFieldAction.ReplaceByEmpty;
					csv.DefaultParseErrorAction = ParseErrorAction.AdvanceToNextLine;

					int fieldCount = csv.FieldCount;
					string[] headers = csv.GetFieldHeaders();

					while (csv.ReadNextRecord())
					{
						var user = new CsvUser();

						if (user.Read(csv))
							if (users.ContainsKey(user.Name) == false)
								users.Add(user.Name, user);
					}
				}
			}
			catch (Exception)
			{
				// должно вызываться снаружи лока
				//OnError(ex.Message);
			}
			finally
			{
				sync.ExitWriteLock();
			}

			OnReset(accountId);
		}