#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Clients.Access;
using HeuristicLab.Clients.Hive.WebJobManager.Services;
using System;
using System.Collections.Generic;
using System.ServiceModel.Security;
namespace HeuristicLab.Clients.Hive.WebJobManager.Models
{
public class HiveWebUser
{
private WebLoginService weblog;
private string username;
public bool OKBOnly { get; set; }
public Guid webIdToken { get; set; }
public User currentUser { get; set; }
public List subscribedGroups { get; set; }
public List accessRoles { get; set; }
public HiveWebUser(Guid token, string username, bool okb)
{
webIdToken = token;
this.username = username;
this.weblog = WebLoginService.Instance;
OKBOnly = okb;
updateUserInfo();
}
public HiveWebUser updateUserInfo()
{
try
{
if (OKBOnly)
throw new SecurityAccessDeniedException();
var access = weblog.getAccessAdminClient(webIdToken);
access.RefreshUsers();
access.RefreshUserGroups();
access.RefreshRoles();
currentUser = access.Users.Find(x => x.UserName == username);
subscribedGroups = access.CallAccessService(x => x.GetUserGroupsOfUser(currentUser.Id));
accessRoles = access.CallAccessService(x => x.GetRolesOfCurrentUser());
}
catch (Exception e)
{
if (e is SecurityAccessDeniedException || e is NullReferenceException)
{
currentUser = new User();
currentUser.FullName = username;
subscribedGroups = new List();
accessRoles = new List();
}
else
throw e;
}
return this;
}
public bool HasUserAdminAccess()
{
if (accessRoles.Find(x => x.Name == "AccessService Administrator") != null || weblog.getAccessAdminClient(webIdToken) != null)
{
return true;
}
return false;
}
public bool hasResourceAdminAccess()
{
if ((accessRoles.Find(x => x.Name == "Hive Administrator") != null
&& accessRoles.Find(x => x.Name == "AccessService Administrator") != null))
{
return true;
}
return false;
}
public bool hasOKBAccess()
{
if (OKBOnly || accessRoles.Find(x => x.Name == "OKB User") != null || accessRoles.Find(x => x.Name == "OKB Administrator") != null
|| weblog.getQueryClient(webIdToken) != null)
{
return true;
}
return false;
}
public bool hasOKBAdminAccess()
{
if (OKBOnly)
{
if (weblog.getOkbAdminClient(webIdToken) != null)
return true;
}
else if (accessRoles.Find(x => x.Name == "OKB Administrator") != null || weblog.getOkbAdminClient(webIdToken) != null)
{
return true;
}
return false;
}
}
}