83 lines
3.9 KiB
C#
83 lines
3.9 KiB
C#
using bts;
|
|
using bts.api.admin;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.Composition;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Reactive.Subjects;
|
|
using bts.api.core;
|
|
using mbts.ui.shell;
|
|
|
|
namespace mbts.spark {
|
|
[Export]
|
|
class AdminModel : IDisposable {
|
|
public readonly UserTuple user;
|
|
|
|
readonly BehaviorSubject<IReadOnlyDictionary<uint, string>>
|
|
_accounts_subject = new BehaviorSubject<IReadOnlyDictionary<uint, string>>(new Dictionary<uint, string>()),
|
|
_users_subject = new BehaviorSubject<IReadOnlyDictionary<uint, string>>(new Dictionary<uint, string>());
|
|
IReadOnlyDictionary<uint, string>
|
|
_account_map = new Dictionary<uint, string>(),
|
|
_user_map = new Dictionary<uint, string>();
|
|
readonly BehaviorSubject<AdminModelWrapper> _wrapper_subject = new BehaviorSubject<AdminModelWrapper>(new AdminModelWrapper());
|
|
IDisposable _dispose;
|
|
|
|
[ImportingConstructor]
|
|
AdminModel(AdminService service, [Import]UserTuple user) {
|
|
this.user = user;
|
|
_dispose = service.subscribei_admin_data().Select(update => {
|
|
_wrapper_subject.OnNext(new AdminModelWrapper(update.all.TryFirst(), user.name));
|
|
IEnumerable<BTSUserImmutable> active_users =
|
|
from admin_data in update.all
|
|
from bts_user in admin_data.Users
|
|
where bts_user.Name == user.name && bts_user.HasUserData && bts_user.UserData.HasAccountPermissions
|
|
select bts_user;
|
|
|
|
IEnumerable<BTSAccountImmutable> active_accounts =
|
|
from admin_data in update.all
|
|
from bts_account in admin_data.Accounts
|
|
where bts_account.IsPresent && _wrapper_subject.Value.can_view_account(bts_account.Name)
|
|
join account_id in (
|
|
from bts_user in active_users
|
|
from account_id in bts_user.UserData.AccountPermissions.AccountPermissions.Keys
|
|
select account_id
|
|
) on bts_account.Id equals account_id
|
|
select bts_account;
|
|
|
|
var users = active_users.ToDictionary(u => u.Id, u => u.Name);
|
|
var accounts = active_accounts.ToDictionary(a => a.Id, a => a.Name);
|
|
return (users, accounts);
|
|
}).Subscribe(t => {
|
|
_account_map = t.accounts;
|
|
_user_map = t.users;
|
|
_users_subject.OnNext(_user_map = t.users);
|
|
_accounts_subject.OnNext(_account_map = t.accounts);
|
|
});
|
|
}
|
|
|
|
public IObservable<IReadOnlyDictionary<uint, string>> on_users => _users_subject.AsObservable();
|
|
public IObservable<IReadOnlyDictionary<uint, string>> on_accounts => _accounts_subject.AsObservable();
|
|
public IObservable<IReadOnlyList<UserTuple>> on_user_tuples => on_users.Select(d => d.Select(kv => new UserTuple(kv.Key, kv.Value)).ToList());
|
|
public IObservable<IReadOnlyList<AccountTuple>> on_account_tuples => on_accounts.Select(d => d.Select(kv => new AccountTuple(kv.Key, kv.Value)).ToList());
|
|
public IObservable<AdminModelWrapper> on_admin_data => _wrapper_subject.AsObservable();
|
|
|
|
public AccountTuple? get_default_account() {
|
|
foreach (var kv in _account_map) {
|
|
return new AccountTuple(kv.Key, kv.Value);
|
|
}
|
|
return null;
|
|
}
|
|
public uint? get_user_id(string user) => _id_for_name(user, _user_map);
|
|
public uint? get_account_id(string account) => _id_for_name(account, _account_map);
|
|
public void Dispose() => Disposable.dispose(ref _dispose);
|
|
|
|
static uint? _id_for_name(string name, IReadOnlyDictionary<uint, string> map) {
|
|
foreach (var kv in map) {
|
|
if (kv.Value == name)
|
|
return kv.Key;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |