private UserManagementUsersViewModel MapEmbyUser(EmbyUser embyInfo, EmbyUsers dbUser, DateTime lastLoggedIn)
        {
            var newUser = false;

            if (dbUser == null)
            {
                newUser = true;
                dbUser  = new EmbyUsers();
            }
            var features    = (Features)dbUser?.Features;
            var permissions = (Permissions)dbUser?.Permissions;

            var m = new UserManagementUsersViewModel
            {
                Id = embyInfo.Id,
                PermissionsFormattedString = newUser ? "Processing..." : (permissions == 0 ? "None" : permissions.ToString()),
                FeaturesFormattedString    = newUser ? "Processing..." : features.ToString(),
                Username     = embyInfo.Name,
                Type         = UserType.EmbyUser,
                EmailAddress = dbUser.EmailAddress,
                Alias        = dbUser?.UserAlias ?? string.Empty,
                LastLoggedIn = lastLoggedIn,
                ManagedUser  = false
            };

            m.Permissions.AddRange(GetPermissions(permissions));
            m.Features.AddRange(GetFeatures(features));

            return(m);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the username this could be the alias! We should always use this method when getting the username
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="session"></param>
        /// <returns>
        ///   <c>null</c> if we cannot find a user
        /// </returns>
        public string GetUsername(string username, ISession session)
        {
            var plexUser = PlexUsers.GetUserByUsername(username);

            if (plexUser != null)
            {
                return(!string.IsNullOrEmpty(plexUser.UserAlias) ? plexUser.UserAlias : plexUser.Username);
            }

            var embyUser = EmbyUsers.GetUserByUsername(username);

            if (embyUser != null)
            {
                return(!string.IsNullOrEmpty(embyUser.UserAlias) ? embyUser.UserAlias : embyUser.Username);
            }

            var dbUser = UserRepository.GetUserByUsername(username);

            if (dbUser != null)
            {
                var userProps = ByteConverterHelper.ReturnObject <UserProperties>(dbUser.UserProperties);
                return(!string.IsNullOrEmpty(userProps.UserAlias) ? userProps.UserAlias : dbUser.UserName);
            }

            // could be a local user
            var hasSessionKey = session[SessionKeys.UsernameKey] != null;

            if (hasSessionKey)
            {
                return((string)session[SessionKeys.UsernameKey]);
            }

            return(string.Empty);
        }
Beispiel #3
0
        private Permissions GetPermissions(string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                // Username without auth
                var s = UserManagementSettings.GetSettings();
                return((Permissions)UserManagementHelper.GetPermissions(s));
            }

            var dbUser = UserRepository.GetUserByUsername(userName);

            if (dbUser != null)
            {
                var permissions = (Permissions)dbUser.Permissions;
                return(permissions);
            }

            var plexUser = PlexUsers.GetUserByUsername(userName);

            if (plexUser != null)
            {
                var permissions = (Permissions)plexUser.Permissions;
                return(permissions);
            }

            var embyUsers = EmbyUsers.GetUserByUsername(userName);

            if (embyUsers != null)
            {
                return((Permissions)embyUsers.Permissions);
            }

            return(0);
        }
Beispiel #4
0
        public void Start()
        {
            JobRecord.SetRunning(true, JobNames.EmbyUserChecker);

            try
            {
                var settings = EmbySettings.GetSettings();
                if (string.IsNullOrEmpty(settings.ApiKey) || !settings.Enable)
                {
                    return;
                }
                var embyUsers = EmbyApi.GetUsers(settings.FullUri, settings.ApiKey);
                var userManagementSettings = UserManagementSettings.GetSettings();

                var dbUsers = Repo.GetAll().ToList();

                // Regular users
                foreach (var user in embyUsers)
                {
                    var dbUser = dbUsers.FirstOrDefault(x => x.EmbyUserId == user.Id);
                    if (dbUser != null)
                    {
                        // we already have a user
                        continue;
                    }

                    // Looks like it's a new user!
                    var m = new EmbyUsers
                    {
                        EmbyUserId  = user.Id,
                        Permissions = UserManagementHelper.GetPermissions(userManagementSettings),
                        Features    = UserManagementHelper.GetFeatures(userManagementSettings),
                        UserAlias   = string.Empty,
                        Username    = user.Name,
                        LoginId     = Guid.NewGuid().ToString()
                    };


                    // If it's the admin, give them the admin permission
                    if (user.Policy.IsAdministrator)
                    {
                        if (!((Permissions)m.Permissions).HasFlag(Permissions.Administrator))
                        {
                            m.Permissions += (int)Permissions.Administrator;
                        }
                    }

                    Repo.Insert(m);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                JobRecord.SetRunning(false, JobNames.EmbyUserChecker);
                JobRecord.Record(JobNames.EmbyUserChecker);
            }
        }
Beispiel #5
0
        public bool IsExternalUser(IUserIdentity user)
        {
            if (user == null)
            {
                return(false);
            }
            var plexUser = PlexUsers.GetUserByUsername(user.UserName);
            var embyUser = EmbyUsers.GetUserByUsername(user.UserName);

            return(plexUser != null || embyUser != null);
        }