2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-07-11 15:02:35 +02:00
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Core;
|
2018-07-27 19:18:05 +02:00
|
|
|
|
using XenAdmin.Network;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
|
|
|
|
public class AddRemoveRolesAction : PureAsyncAction
|
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2020-07-11 15:02:35 +02:00
|
|
|
|
private readonly List<Role> _newRoles;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private readonly Subject subject;
|
|
|
|
|
|
2020-07-11 15:02:35 +02:00
|
|
|
|
public AddRemoveRolesAction(IXenConnection connection, Subject subject, List<Role> newRoles)
|
2018-07-27 19:18:05 +02:00
|
|
|
|
: base(connection,
|
|
|
|
|
string.Format(Messages.AD_ADDING_REMOVING_ROLES_ON, (subject.DisplayName ?? subject.SubjectName ?? subject.subject_identifier).Ellipsise(50)),
|
|
|
|
|
Messages.AD_ADDING_REMOVING_ROLES,
|
|
|
|
|
false)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-07-27 19:18:05 +02:00
|
|
|
|
var pool = Helpers.GetPool(connection);
|
|
|
|
|
if (pool != null)
|
|
|
|
|
Pool = pool;
|
|
|
|
|
else
|
2021-08-31 12:31:16 +02:00
|
|
|
|
Host = Helpers.GetCoordinator(connection);
|
2020-07-11 15:02:35 +02:00
|
|
|
|
|
|
|
|
|
_newRoles = newRoles;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
this.subject = subject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
2022-01-29 02:40:37 +01:00
|
|
|
|
var serverRoles = Connection.Cache.Roles
|
2022-02-28 22:12:09 +01:00
|
|
|
|
.Where(r => (!Helpers.Post82X(Connection) || !Helpers.XapiEqualOrGreater_22_5_0(Connection) || !r.is_internal) && r.subroles.Count > 0)
|
2022-01-29 02:40:37 +01:00
|
|
|
|
.ToList();
|
2020-07-11 15:02:35 +02:00
|
|
|
|
|
|
|
|
|
var toAdd = serverRoles.Where(role => _newRoles.Contains(role) &&
|
|
|
|
|
!subject.roles.Contains(new XenRef<Role>(role.opaque_ref))).ToList();
|
|
|
|
|
|
|
|
|
|
var toRemove = serverRoles.Where(role => !_newRoles.Contains(role) &&
|
|
|
|
|
subject.roles.Contains(new XenRef<Role>(role.opaque_ref))).ToList();
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int count = toAdd.Count + toRemove.Count;
|
|
|
|
|
int done = 0;
|
2019-11-08 21:56:04 +01:00
|
|
|
|
var subj = subject.DisplayName ?? subject.SubjectName ?? subject.subject_identifier;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
foreach (Role r in toAdd)
|
|
|
|
|
{
|
2020-07-11 15:02:35 +02:00
|
|
|
|
log.DebugFormat("Adding role {0} to subject '{1}'.", r.FriendlyName(), subj);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Subject.add_to_roles(Session, subject.opaque_ref, r.opaque_ref);
|
2020-07-11 15:02:35 +02:00
|
|
|
|
PercentComplete = 100 * ++done / count;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Role r in toRemove)
|
|
|
|
|
{
|
2020-07-11 15:02:35 +02:00
|
|
|
|
log.DebugFormat("Removing role {0} from subject '{1}'.", r.FriendlyName(), subj);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Subject.remove_from_roles(Session, subject.opaque_ref, r.opaque_ref);
|
2020-07-11 15:02:35 +02:00
|
|
|
|
PercentComplete = 100 * ++done / count;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2020-07-11 15:02:35 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Description = Messages.COMPLETED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|