/// <summary>
        /// Gets the user from the databas
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="passwordHash">The password hash.</param>
        /// <returns>the users token</returns>
        private async Task <string> GetUser(string user, string passwordHash)
        {
            string filter = string.Format("LoginName eq '{0}' and passwordHash eq '{1}'", user, passwordHash);
            var    result = CommonRepositoryHandler.GetListByQuery(typeof(NetworkUser), filter, false).Cast <NetworkUser>().FirstOrDefault();

            return(await Task.FromResult(result?.LoginName));
        }
Esempio n. 2
0
        /// <summary>
        /// Checks the default user.
        /// </summary>
        /// <param name="loginName">Name of the login.</param>
        /// <param name="pass">The pass.</param>
        /// <param name="secret">The secret.</param>
        /// <returns></returns>
        private async Task <NetworkUser> CheckUser(string loginName, string pass, string secret)
        {
            try
            {
                string filter = nameof(NetworkUser.LoginName) + " eq '" + loginName + "'";
                var    user   = CommonRepositoryHandler.GetListByQuery(typeof(NetworkUser), filter, false).Cast <NetworkUser>().FirstOrDefault();

                if (user == null)
                {
                    user = new NetworkUser()
                    {
                        Id           = Guid.NewGuid().ToString(),
                        IsActive     = true,
                        IsAdmin      = true,
                        LoginName    = loginName,
                        Name         = loginName,
                        PasswordHash = pass.Hash(secret),
                        CreatedDT    = DateTime.UtcNow.ToISO8601(),
                        ModifiedDT   = DateTime.UtcNow.ToISO8601()
                    };
                    await CommonRepositoryHandler.CreateData(typeof(NetworkUser), user);
                }

                return(user);
            }
            catch (Exception ex)
            {
                throw new Exception("Could not create default User: " + ex.Message);
            }
        }
        /// <summary>
        /// Processes the fetch request
        /// </summary>
        /// <param name="req">The req<see cref="FetchRequest" /></param>
        /// <param name="streamId">The streamId<see cref="string" /></param>
        /// <param name="token">The token<see cref="string" /></param>
        /// <param name="header">The header<see cref="Metadata" /></param>
        /// <param name="checkForAuthentication">if set to <c>true</c> [check for authentication].</param>
        /// <returns>The <see cref="Task{GrpcResponse}"/></returns>
        public override async Task <GrpcResponse> ProcessFetch(FetchRequest req, string streamId, string token, Metadata header, bool checkForAuthentication = true)
        {
            try
            {
                if (!this.authenticationHandler.TokenIsValid(token))
                {
                    return(Unauthorized());
                }

                string acceptedType = Any.GetTypeName(Any.Pack(new NetworkUser()).TypeUrl);
                if (string.IsNullOrEmpty(req.TypeDescription) ||
                    req.TypeDescription != acceptedType)
                {
                    throw new Exception("invalid modeltype for fetch request");
                }

                Log("Processing fetch request for type: " + req.TypeDescription, LogLevel.Trace);

                System.Type type       = CommonRepositoryHandler.GetAllRepositories().FirstOrDefault(p => p.ProtoTypeUrl == req.TypeDescription).ModelType;
                bool        includeAll = req.InlcudeRelatedEntities;
                string      query      = req.Query;

                var result = CommonRepositoryHandler.GetListByQuery(type, query, includeAll).Cast <IMessage>();
                return(await Task.FromResult(Ok(result.ToList())));
            }
            catch (Exception ex)
            {
                Log("Fetch error", ex);
                return(BadRequest(ex));
            }
        }
        /// <summary>
        /// Gets an entitiy by id. If include all is set to true, all relational data from other services will
        /// be included at the object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id">The identifier.</param>
        /// <param name="includeAll">if set to <c>true</c> [include all].</param>
        /// <returns>the requeted entity</returns>
        public async Task <T> GetById <T>(Guid id, bool includeAll = true) where T : class, IBaseModel
        {
            T result = default(T);

            if (CommonRepositoryHandler.GetRepository(typeof(T)) != null)
            {
                result = CommonRepositoryHandler.GetById(typeof(T), id, includeAll) as T;
            }

            return(await GetRelationalData(result, includeAll));
        }
 /// <summary>
 /// Register all repositories dynamicaly by the given context.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public static void RegisterRepositories <T>() where T : BaseContext
 {
     try
     {
         var entityTypes = ((Activator.CreateInstance(typeof(T))) as BaseContext).Model.GetEntityTypes();
         foreach (var type in entityTypes)
         {
             if (!CommonRepositoryHandler.GetAllRepositories().Any(p => p.ModelType == type.ClrType))
             {
                 Type repositoryType = typeof(Repository <,>).MakeGenericType(type.ClrType, typeof(T));
                 var  repository     = (IRepository)Activator.CreateInstance(repositoryType);
                 CommonRepositoryHandler.Register(repository);
             }
         }
     }
     catch (Exception ex)
     {
         throw new Exception("Failed to register repositories - see inner exception for details.", ex);
     }
 }