You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/Ombi.Core/Users/UserHelper.cs

255 lines
10 KiB

#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: UserHelper.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Linq;
8 years ago
using Ombi.Core.Models;
using Ombi.Helpers;
using Ombi.Helpers.Permissions;
8 years ago
using Ombi.Store.Models.Emby;
8 years ago
using Ombi.Store.Models.Plex;
8 years ago
using Ombi.Store.Repository;
8 years ago
namespace Ombi.Core.Users
{
public class UserHelper : IUserHelper
{
8 years ago
public UserHelper(IUserRepository userRepository, IExternalUserRepository<PlexUsers> plexUsers, IExternalUserRepository<EmbyUsers> emby, ISecurityExtensions security)
{
LocalUserRepository = userRepository;
PlexUserRepository = plexUsers;
Security = security;
8 years ago
EmbyUserRepository = emby;
}
private IUserRepository LocalUserRepository { get; }
8 years ago
private IExternalUserRepository<PlexUsers> PlexUserRepository { get; }
private ISecurityExtensions Security { get; }
8 years ago
private IExternalUserRepository<EmbyUsers> EmbyUserRepository { get; }
public UserHelperModel GetUser(string username)
{
var localUsers = LocalUserRepository.GetUserByUsername(username);
if (localUsers != null)
{
var props = ByteConverterHelper.ReturnObject<UserProperties>(localUsers.UserProperties);
return new UserHelperModel
{
Type = UserType.LocalUser,
Username = localUsers.UserName,
UserAlias = props.UserAlias,
EmailAddress = props.EmailAddress,
Permissions = (Permissions) localUsers.Permissions,
UserId = localUsers.UserGuid,
Features = (Features)localUsers.Features
};
}
var plexUsers = PlexUserRepository.GetUserByUsername(username);
if (plexUsers != null)
{
return new UserHelperModel
{
Type = UserType.PlexUser,
Username = plexUsers.Username,
UserAlias = plexUsers.UserAlias,
EmailAddress = plexUsers.EmailAddress,
Permissions = (Permissions)plexUsers.Permissions,
UserId = plexUsers.PlexUserId,
Features = (Features)plexUsers.Features
};
}
var embyUsers = EmbyUserRepository.GetUserByUsername(username);
if (embyUsers != null)
{
return new UserHelperModel
{
Type = UserType.EmbyUser,
Username = embyUsers.Username,
UserAlias = embyUsers.UserAlias,
EmailAddress = embyUsers.EmailAddress,
Permissions = (Permissions)embyUsers.Permissions,
UserId = embyUsers.EmbyUserId,
Features = (Features)embyUsers.Features
};
}
return null;
}
public IEnumerable<UserHelperModel> GetUsers()
{
var model = new List<UserHelperModel>();
var localUsers = LocalUserRepository.GetAll();
8 years ago
var plexUsers = PlexUserRepository.GetAll().ToList();
var embyUsers = EmbyUserRepository.GetAll().ToList();
foreach (var user in localUsers)
{
var props = ByteConverterHelper.ReturnObject<UserProperties>(user.UserProperties);
model.Add(new UserHelperModel
{
Type = UserType.LocalUser,
Username = user.UserName,
UserAlias = props.UserAlias,
EmailAddress = props.EmailAddress,
Permissions = (Permissions)user.Permissions
});
}
8 years ago
if (plexUsers.Any())
{
8 years ago
model.AddRange(plexUsers.Select(user => new UserHelperModel
{
Type = UserType.PlexUser,
Username = user.Username,
UserAlias = user.UserAlias,
EmailAddress = user.EmailAddress,
Permissions = (Permissions) user.Permissions
}));
}
if (embyUsers.Any())
{
model.AddRange(embyUsers.Select(user => new UserHelperModel
{
Type = UserType.EmbyUser,
Username = user.Username,
UserAlias = user.UserAlias,
EmailAddress = user.EmailAddress,
Permissions = (Permissions)user.Permissions
}));
}
return model;
}
public IEnumerable<UserHelperModel> GetUsersWithPermission(Permissions permission)
{
var model = new List<UserHelperModel>();
var localUsers = LocalUserRepository.GetAll().ToList();
var plexUsers = PlexUserRepository.GetAll().ToList();
8 years ago
var embyUsers = EmbyUserRepository.GetAll().ToList();
var filteredLocal = localUsers.Where(x => ((Permissions)x.Permissions).HasFlag(permission));
var filteredPlex = plexUsers.Where(x => ((Permissions)x.Permissions).HasFlag(permission));
8 years ago
var filteredEmby = embyUsers.Where(x => ((Permissions)x.Permissions).HasFlag(permission));
foreach (var user in filteredLocal)
{
var props = ByteConverterHelper.ReturnObject<UserProperties>(user.UserProperties);
model.Add(new UserHelperModel
{
Type = UserType.LocalUser,
Username = user.UserName,
UserAlias = props.UserAlias,
EmailAddress = props.EmailAddress,
Permissions = (Permissions)user.Permissions,
Features = (Features)user.Features
});
}
model.AddRange(filteredPlex.Select(user => new UserHelperModel
{
8 years ago
Type = UserType.PlexUser,
Username = user.Username,
UserAlias = user.UserAlias,
EmailAddress = user.EmailAddress,
Permissions = (Permissions)user.Permissions,
Features = (Features)user.Features
}));
model.AddRange(filteredEmby.Select(user => new UserHelperModel
{
Type = UserType.EmbyUser,
Username = user.Username,
UserAlias = user.UserAlias,
EmailAddress = user.EmailAddress,
Permissions = (Permissions)user.Permissions,
Features = (Features)user.Features
}));
8 years ago
return model;
}
8 years ago
public IEnumerable<UserHelperModel> GetUsersWithFeature(Features features)
{
var model = new List<UserHelperModel>();
var localUsers = LocalUserRepository.GetAll().ToList();
var plexUsers = PlexUserRepository.GetAll().ToList();
var embyUsers = EmbyUserRepository.GetAll().ToList();
var filteredLocal = localUsers.Where(x => ((Features)x.Features).HasFlag(features));
var filteredPlex = plexUsers.Where(x => ((Features)x.Features).HasFlag(features));
8 years ago
var filteredEmby = embyUsers.Where(x => ((Features)x.Features).HasFlag(features));
foreach (var user in filteredLocal)
{
var props = ByteConverterHelper.ReturnObject<UserProperties>(user.UserProperties);
model.Add(new UserHelperModel
{
Type = UserType.LocalUser,
Username = user.UserName,
UserAlias = props.UserAlias,
EmailAddress = props.EmailAddress,
Permissions = (Permissions)user.Permissions,
Features = (Features)user.Features
});
}
model.AddRange(filteredPlex.Select(user => new UserHelperModel
{
8 years ago
Type = UserType.PlexUser,
Username = user.Username,
UserAlias = user.UserAlias,
EmailAddress = user.EmailAddress,
Permissions = (Permissions)user.Permissions,
Features = (Features)user.Features
}));
model.AddRange(filteredEmby.Select(user => new UserHelperModel
{
Type = UserType.EmbyUser,
Username = user.Username,
UserAlias = user.UserAlias,
EmailAddress = user.EmailAddress,
Permissions = (Permissions)user.Permissions,
Features = (Features)user.Features
}));
return model;
}
}
}