Beispiel #1
0
        public async Task <HttpResponseMessage> CreateSystemApp(dynamic appdata)
        {
            Model.systemapp app = new systemapp();

            app = await JsonConvert.DeserializeObjectAsync <systemapp>(appdata);



            if (app == null)
            {
                throw new ArgumentException("You have to pass a systemapp object.", "app");
            }

            using (var userapps = new Model.userappsEntities())
            {
                if (userapps.systemapps.Any(a => a.appname.ToLower().
                                            Equals(app.appname.ToLower())))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "System app already exists"));
                }

                else
                {
                    try
                    {
                        userapps.ChangeTracker.DetectChanges();
                        userapps.systemapps.Add(app);
                        await userapps.SaveChangesAsync();
                    }

                    catch (Exception ex)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                           String.Format("Database error. Exception:{1}", ex.Message)));
                    }

                    return(Request.CreateResponse <Model.systemapp>(app));
                }
            }
        }
Beispiel #2
0
        public async Task <HttpResponseMessage> DeleteSystemApp([FromBody] string appName)
        {
            if (String.IsNullOrEmpty(appName))
            {
                throw new ArgumentException("You have to pass a systemapp object.", "app");
            }

            using (var userapps = new Model.userappsEntities())
            {
                if (!userapps.systemapps.Any(a => a.appname.ToLower().
                                             Equals(appName.ToLower())))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "System app does not exist"));
                }

                else
                {
                    try
                    {
                        userapps.ChangeTracker.DetectChanges();
                        var sysApp = userapps.systemapps.Where(sa => sa.appname.ToLower().Equals(
                                                                   appName.ToLower())).FirstOrDefault();
                        userapps.systemapps.Remove(sysApp);
                        await userapps.SaveChangesAsync();
                    }

                    catch (Exception ex)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                           String.Format("Database error. Exception:{1}", ex.Message)));
                    }

                    return(Request.CreateResponse <string>(appName));
                }
            }
        }
Beispiel #3
0
        public async Task <HttpResponseMessage> AddSystemAppUser(IncomingData appData)
        {
            int userId   = appData.systemuserid;
            int sysappid = appData.appid;

            if (userId == 0 || userId <= 0)
            {
                throw new ArgumentException("You have to pass a systemapp id.", "sysappid");
            }

            if (sysappid == null || sysappid <= 0)
            {
                throw new ArgumentException("You have to pass a a valid username", "userId");
            }

            using (var userapps = new Model.userappsEntities())
            {
                using (var system = new Model.exgripEntities())
                {
                    if (!userapps.systemapps.Any(a => a.id == sysappid))
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                           "System application does not exist"));
                    }

                    else
                    {
                        userapps.ChangeTracker.DetectChanges();
                        var systemUser = system.UserProfiles.Where(sus => sus.UserId == userId).FirstOrDefault();
                        var sysAppUser = userapps.systemappusers.Where(us => us.systemuserid == systemUser.UserId).FirstOrDefault();
                        Model.systemappuser sysAppUserEntry = null;

                        if (sysAppUser == null)
                        {
                            try
                            {
                                var password = Membership.GeneratePassword(10, 3);
                                //Generate authentication data
                                UserAppAuthenticationManager authManger = new Security.UserAppAuthenticationManager();
                                var user = await authManger.IssueTokenSysApp(systemUser.UserName, password, systemUser.UserId, sysappid);

                                sysAppUserEntry = new Model.systemappuser()
                                {
                                    systemuserid = systemUser.UserId,
                                    appSecret    = user.Secret,
                                    apptoken     = user.Token,
                                    appid        = sysappid,
                                    securitySoup = user.SecSoup
                                };
                                userapps.systemappusers.Add(sysAppUserEntry);
                                await userapps.SaveChangesAsync();
                            }

                            catch (Exception ex)
                            {
                                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   String.Format("Database error. Exception:{1}", ex.Message)));
                            }

                            return(Request.CreateResponse <Model.systemappuser>(sysAppUserEntry));
                        }

                        else
                        {
                            try
                            {
                                var password = Membership.GeneratePassword(10, 3);
                                //Generate authentication data
                                UserAppAuthenticationManager authManger = new Security.UserAppAuthenticationManager();
                                var user = await authManger.IssueTokenSysApp(systemUser.UserName, password, systemUser.UserId, sysappid);


                                sysAppUser.appSecret    = user.Secret;
                                sysAppUser.apptoken     = user.Token;
                                sysAppUser.securitySoup = user.SecSoup;


                                await userapps.SaveChangesAsync();
                            }

                            catch (Exception ex)
                            {
                                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   String.Format("Database error. Exception:{1}", ex.Message)));
                            }

                            return(Request.CreateResponse <Model.systemappuser>(sysAppUser));
                        }
                    }
                }
            }
        }
Beispiel #4
0
        public async Task <HttpResponseMessage> DeleteSystemAppUser(dynamic data)
        {
            if (data.appName == null)
            {
                throw new ArgumentException("You have to pass a systemapp object.", "app");
            }

            if (data.userName == null)
            {
                throw new ArgumentException("You have to pass a a valid username", "userName");
            }

            string userName = data.userName;
            string appname  = data.appName;

            using (var userapps = new Model.userappsEntities())
            {
                using (var system = new Model.exgripEntities())
                {
                    if (!userapps.systemapps.Any(a => a.appname.ToLower().
                                                 Equals(appname.ToLower())))
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                           "System application does not exist"));
                    }

                    else
                    {
                        var sysAppUser = system.UserProfiles.Where(sus =>
                                                                   sus.UserName.ToLower().Equals(userName.ToLower())).FirstOrDefault();
                        var sysApp = userapps.systemapps.Where(a => a.appname.ToLower().
                                                               Equals(appname.ToLower())).FirstOrDefault();

                        if (sysAppUser == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "System user does not exist"));
                        }

                        else
                        {
                            Model.systemappuser sysUserInApp = null;

                            try
                            {
                                userapps.ChangeTracker.DetectChanges();
                                sysUserInApp = userapps.systemappusers.Where(
                                    sa => sa.appid == sysApp.id && sa.systemuserid == sysAppUser.UserId).FirstOrDefault();

                                if (sysUserInApp == null)
                                {
                                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                       "System app user cannot be found."));
                                }

                                userapps.systemappusers.Remove(sysUserInApp);
                                await userapps.SaveChangesAsync();
                            }

                            catch (Exception ex)
                            {
                                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   String.Format("Database error. Exception:{0}", ex.Message)));
                            }

                            return(Request.CreateResponse <Model.systemappuser>(sysUserInApp));
                        }
                    }
                }
            }
        }