Ejemplo n.º 1
0
        public HttpResponseMessage ListAllUserApps(dynamic data)
        {
            string userName = data.userName;

            if (String.IsNullOrEmpty(userName))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Username cannot be null. Please supply a username."));
            }

            using (var userApps = new userappsEntities())
            {
                using (var system = new exgripEntities())
                {
                    if (!String.IsNullOrEmpty(userName))
                    {
                        var currentUser = system.UserProfiles.Where(usr => usr.UserName.ToLower().Equals(
                                                                        userName.ToLower())).FirstOrDefault();

                        if (currentUser == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User does not exist."));
                        }

                        else
                        {
                            var appsByUser = userApps.apps.Where(app => app.systemuserid == currentUser.UserId);

                            if (appsByUser == null)
                            {
                                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User has no apps."));
                            }

                            else
                            {
                                if (appsByUser.Count() >= 1)
                                {
                                    return(Request.CreateResponse <List <app> >(appsByUser.ToList()));
                                }

                                else
                                {
                                    var usrApp = appsByUser.FirstOrDefault();

                                    if (usrApp == null)
                                    {
                                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User has no apps so far."));
                                    }

                                    return(Request.CreateResponse <app>(usrApp));
                                }
                            }
                        }
                    }
                }
            }

            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No userapps available"));
        }
        private Task <bool> ValidateAppUser(int appId, int systemUserId)
        {
            #region CheckParameters
            if (appId <= 0)
            {
                throw new ArgumentException("Application id cannot be zero or negative", "appId");
            }

            if (systemUserId <= 0)
            {
                throw new ArgumentException("User id cannot be zero or negative.", "systemUserId");
            }

            #endregion
            TaskCompletionSource <bool> tks = new TaskCompletionSource <bool> ();
            using (var userapps = new userappsEntities())
            {
                userapps.ChangeTracker.DetectChanges();
                using (var sysUsers = new exgripEntities())
                {
                    sysUsers.ChangeTracker.DetectChanges();
                    var sysUser = sysUsers.UserProfiles.Where(usr => usr.UserId == systemUserId).FirstOrDefault();

                    if (sysUser == null)
                    {
                        tks.SetResult(false);
                    }

                    else
                    {
                        var sysApp = userapps.systemapps.Where(sa => sa.id == appId).FirstOrDefault();

                        if (sysApp == null)
                        {
                            tks.SetResult(false);
                        }

                        else
                        {
                            var sysAppUsr = userapps.systemappusers.Any(sau => sau.appid == sysApp.id &&
                                                                        sau.systemuserid == sysUser.UserId);
                            tks.SetResult(sysAppUsr);
                        }
                    }
                }
            }
            return(tks.Task);
        }
Ejemplo n.º 3
0
        public async Task <HttpResponseMessage> CreateUserApp(dynamic data)
        {
            int    systemuserid = data.systemuserid;
            string appName      = data.appName;

            app newApp = null;

            using (var uapps = new userappsEntities())
            {
                using (var sysuser = new exgripEntities())
                {
                    if (sysuser.UserProfiles.Any(u => u.UserId == systemuserid))
                    {
                        if (!uapps.apps.Any(a => a.appname.ToLower().Equals(appName.ToLower())))
                        {
                            try
                            {
                                uapps.ChangeTracker.DetectChanges();

                                newApp = new app()
                                {
                                    appname = appName, systemuserid = systemuserid
                                };

                                uapps.apps.Add(newApp);

                                await uapps.SaveChangesAsync();
                            }

                            catch (Exception ex)
                            {
                                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.ToString()));
                            }
                        }
                    }

                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Systemuser does not exist"));
                    }
                }
            }
            return(Request.CreateResponse <app>(newApp));
        }
        /// <summary>
        /// Validates the application user.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">
        /// Username cannot be null or empty.;username
        /// or
        /// Password cannot be null or empty.;password
        /// </exception>
        private Task <bool> ValidateAppUser(string username, string password, string passwordOrig)
        {
            #region CheckParameters
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Username cannot be null or empty.", "username");
            }

            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Password cannot be null or empty.", "password");
            }

            #endregion
            TaskCompletionSource <bool> tks = new TaskCompletionSource <bool> ();
            using (var sysUser = new exgripEntities())
            {
                var user = sysUser.UserProfiles.Where(u => u.UserName.ToUpper().Equals(
                                                          username.ToUpper())
                                                      ).FirstOrDefault();

                if (user == null)
                {
                    tks.SetResult(false);
                }
                else
                {
                    using (var userapps = new userappsEntities())
                    {
                        var appUserExists = userapps.apps.Any(ua => ua.systemuserid == user.UserId);

                        if (appUserExists)
                        {
                            tks.SetResult(true);
                        }
                        else
                        {
                            tks.SetResult(false);
                        }
                    }
                }
            }
            return(tks.Task);
        }
        /// <summary>
        /// Issues the token.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="systemuserid">The systemuserid.</param>
        /// <param name="appId">The application identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">
        /// Username cannot be null or empty.;username
        /// or
        /// Password cannot be null or empty.;password
        /// or
        /// Userid cannot be zero or negative.;systemuserid
        /// or
        /// Appid cannot be zero or negative.;appId
        /// </exception>
        public async Task <User> IssueToken(int systemuserid, int appId)
        {
            #region CheckParameters
            //if ( String.IsNullOrEmpty ( username ) )
            //{
            //    throw new ArgumentException ( "Username cannot be null or empty.", "username" );
            //}

            //if ( String.IsNullOrEmpty ( password ) )
            //{
            //    throw new ArgumentException ( "Password cannot be null or empty.", "password" );
            //}
            if (systemuserid <= 0)
            {
                throw new ArgumentException("Userid cannot be zero or negative.", "systemuserid");
            }

            if (appId <= 0)
            {
                throw new ArgumentException("Appid cannot be zero or negative.", "appId");
            }

            #endregion
            TaskCompletionSource <User> tks = new TaskCompletionSource <User> ();

            using (var userapps = new userappsEntities())
            {
                using (var sysuser = new exgripEntities())
                {
                    userapps.ChangeTracker.DetectChanges();
                    //Check if app exeists
                    var currentApp = userapps.apps.Where(a => a.idapps == appId &&
                                                         a.systemuserid == systemuserid).FirstOrDefault();
                    var currentUser = sysuser.UserProfiles.Where(usr => usr.UserId ==
                                                                 systemuserid).FirstOrDefault();



                    var password = Membership.GeneratePassword(15, 5);

                    if (currentUser == null)
                    {
                        tks.SetResult(null);
                        return(tks.Task.Result);
                    }

                    if (currentApp != null)
                    {
                        var encrptedPassword = await GeneratePasswordSalt(currentUser.UserName, password);

                        var user = await EncryptToken(currentUser.UserName, encrptedPassword, password, true);

                        userapps.appusers.Add(new appuser()
                        {
                            appSecret    = user.Secret,
                            apptoken     = user.Token,
                            appid        = currentApp.idapps,
                            securitySoup = user.SecSoup
                        });

                        try
                        {
                            await userapps.SaveChangesAsync();
                        }

                        catch (Exception ex)
                        {
                        }

                        tks.SetResult(user);
                    }

                    else
                    {
                        try
                        {
                            var encrptedPassword = await GeneratePasswordSalt(currentUser.UserName, password);

                            var user = await EncryptToken(currentUser.UserName, encrptedPassword, password);

                            //Update existing user
                            var existingUser = userapps.appusers.Where(ua => ua.appid ==
                                                                       currentApp.idapps).FirstOrDefault();
                            existingUser.appSecret    = user.Secret;
                            existingUser.apptoken     = user.Token;
                            existingUser.securitySoup = user.SecSoup;
                            await userapps.SaveChangesAsync();

                            tks.SetResult(user);
                        }

                        catch (Exception ex)
                        {
                            throw;
                        }
                    }
                }
            }
            return(tks.Task.Result);
        }
Ejemplo n.º 6
0
        public async Task <bool> PostCreateNewMultiUserCode(dynamic data)
        {
            const string APP_KEY    = "X-AppKey";
            const string APP_SECRET = "X-Token";

            systemappuser user    = null;
            UserProfile   profile = null;

            if (Request.Headers.Contains(APP_KEY) && Request.Headers.Contains(APP_SECRET))
            {
                string appKey    = Request.Headers.GetValues(APP_KEY).First();
                string appSecret = Request.Headers.GetValues(APP_SECRET).First();

                using (var sysapps = new userappsEntities())
                {
                    user = sysapps.systemappusers.Where(usr => usr.appSecret.Equals(appSecret) && usr.apptoken.Equals(appKey)).FirstOrDefault();


                    if (user == null)
                    {
                        return(false);
                    }
                    else
                    {
                        using (var exgrip = new exgripEntities())
                        {
                            profile = exgrip.UserProfiles.Where(up => up.UserId == user.systemuserid).FirstOrDefault();

                            if (profile == null)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            else
            {
                return(false);
            }

            WordGenerator gen = new WordGenerator();

            var userId         = profile.AlternateUserId;
            var timeZone       = (string)data.timeZone;
            var amountOfUsers  = (int)data.count;
            var dateString     = (string)data.dateString;
            var dateStringFrom = (string)data.dateStringFrom;
            var codeLink       = (string)data.codeLink;
            var count          = (int)data.count;

            int betacount = 200;

            if (string.IsNullOrEmpty(userId) || string.IsNullOrWhiteSpace(userId))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(timeZone) || string.IsNullOrWhiteSpace(timeZone))
            {
                return(false);
            }

            if (amountOfUsers <= 0)
            {
                return(false);
            }

            DateTime outDate;

            var parseResult = DateTime.TryParse(dateString, out outDate);

            if (!parseResult)
            {
                return(false);
            }


            DateTime outDate2;

            var parseResult2 = DateTime.TryParse(dateStringFrom, out outDate2);

            if (!parseResult2)
            {
                return(false);
            }

            if (DateHelpers.GetTimeZoneInfoForTzdbId(timeZone) == null)
            {
                return(false);
            }

            if (!string.IsNullOrWhiteSpace(codeLink))
            {
                Uri  uriResult;
                bool result = Uri.TryCreate(codeLink, UriKind.Absolute, out uriResult);

                if (!result)
                {
                    return(false);
                }
            }

            if (amountOfUsers == 0 || amountOfUsers <= 0 || amountOfUsers > int.MaxValue)
            {
                return(false);
            }

            using (userappsEntities ctx = new userappsEntities())
            {
                try
                {
                    var customerTime = TimeZoneInfo.ConvertTime(new DateTime(outDate.Year, outDate.Month, outDate.Day, outDate.Hour, outDate.Minute, outDate.Second),
                                                                DateHelpers.GetTimeZoneInfoForTzdbId(timeZone),
                                                                DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    var customerTime2 = TimeZoneInfo.ConvertTime(new DateTime(outDate2.Year, outDate2.Month, outDate2.Day, outDate2.Hour, outDate2.Minute, outDate2.Second),
                                                                 DateHelpers.GetTimeZoneInfoForTzdbId(timeZone),
                                                                 DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    var allMultiCodes = ctx.promotioncodes.Where(x => x.userid == userId && x.ismulticode == true).ToList();


                    if ((allMultiCodes.Count()) > betacount)
                    {
                        throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
                    }

                    if (count > 2000000)
                    {
                        throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
                    }

                    if ((customerTime2.Ticks > customerTime.Ticks))
                    {
                        return(false);
                    }

                    var word = gen.RandomString(7);

                    promotioncode code = new promotioncode();
                    code.created = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    code.redeemed          = false;
                    code.promocode         = word;
                    code.userid            = userId;
                    code.timezone          = timeZone;
                    code.multicodequantity = amountOfUsers;
                    code.validfrom         = customerTime2;
                    code.validuntil        = customerTime;
                    code.GetCodeLink       = codeLink;
                    code.IsActive          = true;
                    code.ismulticode       = true;

                    ctx.promotioncodes.Add(code);

                    await ctx.SaveChangesAsync();

                    return(true);
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
        }
Ejemplo n.º 7
0
        public async Task <bool> PostCreateNewStack(dynamic data)
        {
            const string APP_KEY    = "X-AppKey";
            const string APP_SECRET = "X-Token";

            systemappuser user    = null;
            UserProfile   profile = null;

            if (Request.Headers.Contains(APP_KEY) && Request.Headers.Contains(APP_SECRET))
            {
                string appKey    = Request.Headers.GetValues(APP_KEY).First();
                string appSecret = Request.Headers.GetValues(APP_SECRET).First();

                using (var sysapps = new userappsEntities())
                {
                    user = sysapps.systemappusers.Where(usr => usr.appSecret.Equals(appSecret) && usr.apptoken.Equals(appKey)).FirstOrDefault();


                    if (user == null)
                    {
                        return(false);
                    }
                    else
                    {
                        using (var exgrip = new exgripEntities())
                        {
                            profile = exgrip.UserProfiles.Where(up => up.UserId == user.systemuserid).FirstOrDefault();

                            if (profile == null)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            else
            {
                return(false);
            }


            int betaCount = 500;

            WordGenerator gen = new WordGenerator();



            var timeZone       = (string)data.timeZone;
            var dateString     = (string)data.dateString;
            var dateStringFrom = (string)data.dateStringFrom;
            var codeLink       = (string)data.codeLink;
            var userId         = profile.AlternateUserId;
            var count          = (int)data.count;



            DateTime outDate;

            var parseResult = DateTime.TryParse(dateString, out outDate);

            if (!parseResult)
            {
                return(false);
            }

            DateTime outDate2;

            var parseResult2 = DateTime.TryParse(dateStringFrom, out outDate2);

            if (!parseResult2)
            {
                return(false);
            }

            if (count > betaCount)
            {
                return(false);
            }

            using (userappsEntities ctx = new userappsEntities())
            {
                try
                {
                    var customerTime = TimeZoneInfo.ConvertTime(new DateTime(outDate.Year, outDate.Month, outDate.Day, outDate.Hour, outDate.Minute, outDate.Second),
                                                                DateHelpers.GetTimeZoneInfoForTzdbId(timeZone),
                                                                DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    var customerTime2 = TimeZoneInfo.ConvertTime(new DateTime(outDate2.Year, outDate2.Month, outDate2.Day, outDate2.Hour, outDate2.Minute, outDate2.Second),
                                                                 DateHelpers.GetTimeZoneInfoForTzdbId(timeZone),
                                                                 DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    if ((customerTime2.Ticks > customerTime.Ticks))
                    {
                        return(false);
                    }

                    var reedemedVouchers = ctx.promotioncodes.Where(x => x.userid == userId && x.redeemed == true && x.ismulticode == false).ToList();

                    var allOnetimes = ctx.promotioncodes.Where(x => x.userid == userId && x.ismulticode == false).ToList();

                    if ((count + allOnetimes.Count()) > betaCount)
                    {
                        throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
                    }

                    if ((allOnetimes.Count() == betaCount) && (reedemedVouchers.Count < betaCount) && (reedemedVouchers.Count != 0))
                    {
                        return(false);
                    }
                    else
                    {
                        ctx.Configuration.AutoDetectChangesEnabled = false;
                        ctx.Configuration.ValidateOnSaveEnabled    = false;

                        for (int i = 1; i <= count; i++)
                        {
                            var word = gen.RandomString(7);

                            promotioncode code = new promotioncode();

                            code.created     = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));
                            code.validfrom   = customerTime2;
                            code.validuntil  = customerTime;
                            code.redeemed    = false;
                            code.promocode   = word;
                            code.userid      = userId;
                            code.ismulticode = false;
                            code.timezone    = timeZone;
                            code.GetCodeLink = codeLink;
                            code.IsActive    = true;
                            ctx.promotioncodes.Add(code);
                        }

                        await ctx.SaveChangesAsync();

                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
        }