mirror of https://github.com/Squidex/squidex.git
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.
70 lines
2.0 KiB
70 lines
2.0 KiB
// ==========================================================================
|
|
// AppClients.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Squidex.Infrastructure;
|
|
|
|
namespace Squidex.Domain.Apps.Write.Apps
|
|
{
|
|
public class AppClients
|
|
{
|
|
private readonly Dictionary<string, AppClient> clients = new Dictionary<string, AppClient>();
|
|
|
|
public IReadOnlyDictionary<string, AppClient> Clients
|
|
{
|
|
get { return clients; }
|
|
}
|
|
|
|
public void Add(string id, string secret)
|
|
{
|
|
ThrowIfFound(id, () => "Cannot add client");
|
|
|
|
clients[id] = new AppClient(secret, id, false);
|
|
}
|
|
|
|
public void Rename(string clientId, string name)
|
|
{
|
|
ThrowIfNotFound(clientId);
|
|
|
|
clients[clientId] = clients[clientId].Rename(name, () => "Cannot rename client");
|
|
}
|
|
|
|
public void Change(string clientId, bool isReader)
|
|
{
|
|
ThrowIfNotFound(clientId);
|
|
|
|
clients[clientId] = clients[clientId].Change(isReader, () => "Cannot change client");
|
|
}
|
|
|
|
public void Revoke(string clientId)
|
|
{
|
|
ThrowIfNotFound(clientId);
|
|
|
|
clients.Remove(clientId);
|
|
}
|
|
|
|
private void ThrowIfNotFound(string clientId)
|
|
{
|
|
if (!clients.ContainsKey(clientId))
|
|
{
|
|
throw new DomainObjectNotFoundException(clientId, "Contributors", typeof(AppDomainObject));
|
|
}
|
|
}
|
|
|
|
private void ThrowIfFound(string clientId, Func<string> message)
|
|
{
|
|
if (clients.ContainsKey(clientId))
|
|
{
|
|
var error = new ValidationError("Client id is alreay part of the app", "Id");
|
|
|
|
throw new ValidationException(message(), error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|