Consolidated users code into kernel

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-07-31 09:28:12 -04:00
parent d4c75e3974
commit 3058b71764
3 changed files with 69 additions and 87 deletions

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using MediaBrowser.Common.Json;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Events;
@ -23,7 +24,6 @@ namespace MediaBrowser.Controller
public static Kernel Instance { get; private set; }
public ItemController ItemController { get; private set; }
public UserController UserController { get; private set; }
public IEnumerable<User> Users { get; private set; }
public Folder RootFolder { get; private set; }
@ -38,6 +38,14 @@ namespace MediaBrowser.Controller
}
}
private string UsersPath
{
get
{
return Path.Combine(ProgramDataPath, "Users");
}
}
/// <summary>
/// Gets the list of currently registered entity resolvers
/// </summary>
@ -53,7 +61,6 @@ namespace MediaBrowser.Controller
Instance = this;
ItemController = new ItemController();
UserController = new UserController(Path.Combine(ProgramDataPath, "Users"));
DirectoryWatchers = new DirectoryWatchers();
ItemController.PreBeginResolvePath += ItemController_PreBeginResolvePath;
@ -117,7 +124,7 @@ namespace MediaBrowser.Controller
private void ReloadUsers()
{
Users = UserController.GetAllUsers();
Users = GetAllUsers();
}
/// <summary>
@ -422,5 +429,64 @@ namespace MediaBrowser.Controller
return list;
}
/// <summary>
/// Gets all users within the system
/// </summary>
private IEnumerable<User> GetAllUsers()
{
if (!Directory.Exists(UsersPath))
{
Directory.CreateDirectory(UsersPath);
}
List<User> list = new List<User>();
foreach (string folder in Directory.GetDirectories(UsersPath, "*", SearchOption.TopDirectoryOnly))
{
User item = GetFromDirectory(folder);
if (item != null)
{
list.Add(item);
}
}
return list;
}
/// <summary>
/// Gets a User from it's directory
/// </summary>
private User GetFromDirectory(string path)
{
string file = Path.Combine(path, "user.js");
return JsonSerializer.DeserializeFromFile<User>(file);
}
/// <summary>
/// Creates a User with a given name
/// </summary>
public User CreateUser(string name)
{
var now = DateTime.Now;
User user = new User()
{
Name = name,
Id = Guid.NewGuid(),
DateCreated = now,
DateModified = now
};
user.Path = Path.Combine(UsersPath, user.Id.ToString());
Directory.CreateDirectory(user.Path);
JsonSerializer.SerializeToFile(user, Path.Combine(user.Path, "user.js"));
return user;
}
}
}

View File

@ -54,7 +54,6 @@
<Compile Include="Resolvers\BaseItemResolver.cs" />
<Compile Include="Resolvers\FolderResolver.cs" />
<Compile Include="Resolvers\VideoResolver.cs" />
<Compile Include="UserController.cs" />
<Compile Include="Xml\BaseItemXmlParser.cs" />
<Compile Include="Xml\FolderXmlParser.cs" />
<Compile Include="Xml\XmlExtensions.cs" />

View File

@ -1,83 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using MediaBrowser.Model.Users;
using MediaBrowser.Common.Json;
namespace MediaBrowser.Controller
{
/// <summary>
/// Manages users within the system
/// </summary>
public class UserController
{
/// <summary>
/// Gets or sets the path to folder that contains data for all the users
/// </summary>
public string UsersPath { get; set; }
public UserController(string usersPath)
{
UsersPath = usersPath;
}
/// <summary>
/// Gets all users within the system
/// </summary>
public IEnumerable<User> GetAllUsers()
{
if (!Directory.Exists(UsersPath))
{
Directory.CreateDirectory(UsersPath);
}
List<User> list = new List<User>();
foreach (string folder in Directory.GetDirectories(UsersPath, "*", SearchOption.TopDirectoryOnly))
{
User item = GetFromDirectory(folder);
if (item != null)
{
list.Add(item);
}
}
return list;
}
/// <summary>
/// Gets a User from it's directory
/// </summary>
private User GetFromDirectory(string path)
{
string file = Path.Combine(path, "user.js");
return JsonSerializer.DeserializeFromFile<User>(file);
}
/// <summary>
/// Creates a User with a given name
/// </summary>
public User CreateUser(string name)
{
var now = DateTime.Now;
User user = new User()
{
Name = name,
Id = Guid.NewGuid(),
DateCreated = now,
DateModified = now
};
user.Path = Path.Combine(UsersPath, user.Id.ToString());
Directory.CreateDirectory(user.Path);
JsonSerializer.SerializeToFile(user, Path.Combine(user.Path, "user.js"));
return user;
}
}
}