public Guid Add(Alarm alarm) { var id = Guid.NewGuid(); _scheduler.StartNew(() => { try { _alarms.Put(id, alarm); } catch (Exception e) { Log.ErrorFormat("Caught unexpected exception: {0}", e); } }); return(id); }
public void PublishPlugin(byte[] plugin, string accessToken, DateTime publishDate) { Log.DebugFormat("Adding plugin ({0} bytes) to repository...", plugin.Length); IPluginPackageIndex pluginIndex; DateTime builtTime; IReadOnlyList <SerializableChange> changes; byte[] icon; using (var stream = new MemoryStream(plugin)) using (var archive = OpenPlugin(stream)) { pluginIndex = archive.Index; changes = archive.LoadChanges(); builtTime = GetBuildTime(archive.ReadAssembly()); icon = archive.ReadIcon()?.ReadToEnd(); } var id = new PluginIdentifier(pluginIndex.Id, pluginIndex.Version); if (!Guid.TryParse(accessToken, out var token)) { throw new InvalidUserTokenException($"'{accessToken}' is not a valid access token."); } using (var transaction = _database.BeginTransaction()) { if (!_usernamesByAccessToken.TryGet(token, out var userName)) { throw new InvalidUserTokenException($"'{accessToken}' is not a valid access token."); } // TODO: Only throw a temper tantrum in case the plugin to be added differs from the plugin stored in this repository if (_pluginDescriptions.ContainsKey(id) || _plugins.ContainsKey(id)) { throw new PluginAlreadyPublishedException($"The plugin '{id}' already exists and cannot be modified."); } var publishedPlugin = new PublishedPlugin(pluginIndex) { Publisher = userName, Identifier = id, BuildDate = builtTime, SizeInBytes = plugin.Length, PublishDate = publishDate, RequiredInterfaces = pluginIndex.ImplementedPluginInterfaces .Select(x => new PluginInterface(x.InterfaceTypename, x.InterfaceVersion)).ToList(), }; _pluginDescriptions.Put(id, publishedPlugin); _plugins.Put(id, plugin); _pluginIcons.Put(id, icon); transaction.Commit(); Log.InfoFormat("Added plugin '{0}' to repository!", id); } }
public Guid AddUser(string username, string email, string accessToken) { Log.DebugFormat("Adding user '{0}, {1}' to repository...", username, email); const string pattern = "^[a-zA-Z_][a-zA-Z0-9_]*$"; var regex = new Regex(pattern, RegexOptions.Compiled); if (username == null || !regex.IsMatch(username)) { throw new CannotAddUserException($"The username '{username}' does not match the expected pattern: {pattern}"); } if (!IsValidEmail(email)) { throw new CannotAddUserException($"The email '{email}' does not appear to be a valid email address."); } using (var transaction = _database.BeginTransaction()) { if (_users.ContainsKey(username)) { throw new CannotAddUserException($"The user '{username}' already exists and cannot be modified.", isError: false); } Guid token; if (!string.IsNullOrEmpty(accessToken)) { if (!Guid.TryParse(accessToken, out token)) { throw new CannotAddUserException($"The access-token '{accessToken}' is not a valid GUID"); } } else { token = Guid.NewGuid(); } var user = new User { Username = username, Email = email, AccessToken = token }; _users.Put(username, user); _usernamesByAccessToken.Put(token, username); transaction.Commit(); return(token); } }