Exemplo n.º 1
0
        public async Task <IHttpActionResult> DeleteAccount([FromBody] SimpleDeleteModel data)
        {
            Code status   = default(Code);
            int? userId   = UserSessionState.UserID(Convert.ToString(data.token));
            int? response = null;

            if (userId != null)
            {
                var account = store
                              .Users
                              .FirstOrDefault(u => u.UserID == data.id);
                if (account != null)
                {
                    account.IsDeleted = true;
                    store.SaveChanges();
                    response = data.id;
                }
                else
                {
                    status = Code.NotFound;
                }
            }
            else
            {
                status = Code.AuthRequired;
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> IncIssueTool([FromBody] IncIssueModel data)
        {
            Code    status   = default(Code);
            dynamic response = null;
            int?    userID   = UserSessionState.UserID(data.token);

            if (userID.HasValue)
            {
                store.ToolsUses.Add(new ToolsUs()
                {
                    ToolID         = data.id,
                    WorkerID       = data.workerid,
                    ManageWorkerID = store.Users.FirstOrDefault(u => u.WorkerID == userID.Value).WorkerID,
                    Count          = data.count * (data.isinc ? 1 : -1),
                    CreationDate   = DateTimeOffset.UtcNow.DateTime
                });
                store.Audits.Add(new Audit()
                {
                    Action       = (int)(data.isinc ? ToolAction.Take : ToolAction.Issue),
                    Count        = data.count,
                    Readed       = false,
                    ToolID       = data.id,
                    WorkerID     = store.Users.FirstOrDefault(u => u.WorkerID == userID).WorkerID,
                    CreationDate = DateTimeOffset.UtcNow.DateTime,
                });
                store.SaveChanges();
                response = Math.Abs(ToolsHelper.ToolStatByToolID(store, data.id));
            }
            else
            {
                status = Code.AuthRequired;
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> SetAuditMessageReaded([FromBody] MultiDBAction data)
        {
            Code status = default(Code);
            int? userID = UserSessionState.UserID(data.token);

            if (userID.HasValue)
            {
                foreach (var id in data.ids)
                {
                    var audit = store
                                .Audits
                                .FirstOrDefault(a => a.AuditID == id);
                    if (audit != null)
                    {
                        audit.Readed = true;
                        store.SaveChanges();
                    }
                    else
                    {
                        status = Code.NotFound;
                    }
                }
            }
            else
            {
                status = Code.AuthRequired;
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status))));
        }
Exemplo n.º 4
0
        public async Task <IHttpActionResult> UpdateAccount([FromBody] dynamic data)
        {
            Code    status   = default(Code);
            dynamic response = null;

            int?userID = UserSessionState.UserID(data.token);

            if (userID.HasValue)
            {
                int id      = (int)data.id;
                var account = store
                              .Users
                              .FirstOrDefault(u => u.UserID == id);
                if (account != null)
                {
                    account.Email      = data.email;
                    account.UserTypeID = data.typeid;
                    store.SaveChanges();
                }
                else
                {
                    status = Code.NotFound;
                }
            }
            else
            {
                status = Code.AuthRequired;
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }
Exemplo n.º 5
0
        protected void Application_Start()
        {
            UserSessionState.InitializeSession();

            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AreaRegistration.RegisterAllAreas();
        }
Exemplo n.º 6
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var controller = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower();

            if (controller != "tokenapi" /* || (controller.CompareTo("tokenapi") == 0 && actionContext.ActionDescriptor.ActionName.ToLower() != "validateuser")*/)
            {
                var status = default(Code);
                try
                {
                    if (actionContext.ActionArguments != null &&
                        actionContext.Request.Method != HttpMethod.Get)
                    {
                        if (actionContext.Request.Content.IsMimeMultipartContent())
                        {
                            var token = System.Web.HttpContext.Current.Request["token"];
                            if (!UserSessionState.TokenIsValid(token))
                            {
                                status = Code.AuthRequired;
                                throw new Exception();
                            }
                        }
                        else if (actionContext.ActionArguments.Any(p => p.Value == null))
                        {
                            status = Code.BadRequest;
                            throw new Exception();
                        }
                        else
                        {
                            var    model = actionContext.ActionArguments.Where(p => p.Value.GetType().IsClass).Select(p => p.Value).FirstOrDefault();
                            object token = null;
                            if (model is TokenBase)
                            {
                                token = (model as TokenBase).token;
                            }
                            else if (model is Newtonsoft.Json.Linq.JObject)
                            {
                                token = (model as Newtonsoft.Json.Linq.JObject).Value <object>("token");
                            }
                            if (!UserSessionState.TokenIsValid(token))
                            {
                                status = Code.AuthRequired;
                                throw new Exception();
                            }
                        }
                    }
                }
                catch
                {
                    actionContext.Response = actionContext.Request.CreateResponse(
                        HttpStatusCode.OK,
                        ApiResponseManager.CreateResponse(new Status(status)));
                }
            }
            base.OnActionExecuting(actionContext);
        }
Exemplo n.º 7
0
        public async Task <IHttpActionResult> UpdateTool([FromBody] UpdateToolModel data)
        {
            Code    status   = default(Code);
            dynamic response = null;
            var     dbObj    = store
                               .Tools
                               .Where(t => t.ToolID == data.id)
                               .FirstOrDefault();
            int userID = UserSessionState.UserID(data.token).Value;

            if (dbObj != null)
            {
                dbObj.Name   = data.name;
                dbObj.Cell   = data.cell;
                dbObj.Count += data.count;
                if (data.count > 0)
                {
                    store.Audits.Add(new Audit()
                    {
                        Action       = (int)ToolAction.Add,
                        Count        = data.count,
                        Readed       = false,
                        ToolID       = dbObj.ToolID,
                        WorkerID     = store.Users.FirstOrDefault(u => u.WorkerID == userID).WorkerID,
                        CreationDate = DateTimeOffset.UtcNow.DateTime,
                    });
                }
                if (data.low.HasValue)
                {
                    dbObj.LowCount = data.low.Value;
                }
                if (data.lower.HasValue)
                {
                    dbObj.LowerCount = data.lower.Value;
                }
                store.SaveChanges();
                response = new
                {
                    id         = dbObj.ToolID,
                    name       = dbObj.Name,
                    cell       = dbObj.Cell,
                    low        = dbObj.LowCount,
                    lower      = dbObj.LowerCount,
                    toolscount = dbObj.Count,
                    toolsinuse = Math.Abs(ToolsHelper.ToolStatByToolID(store, dbObj.ToolID)),
                };
            }
            else
            {
                status = Code.NotFound;
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }
Exemplo n.º 8
0
 public ActionResult LogOut()
 {
     try
     {
         UserSessionState.LogOut();
         return(Actions.Success());
     }
     catch (Exception ex)
     {
         return(Actions.Error(ex));
     }
 }
Exemplo n.º 9
0
        internal UserSessionState InitialiseUserSessionState(User user, ResponseBuilder responseBuilder)
        {
            if (!SiteState.IsInitalised)
            {
                Assert.Fail("The site state is not yet initialised");
            }

            if (Container == null)
            {
                Container = new Container(new TestConfiguration());
            }

            return(UserSessionState.Initialise(user, MockUsers.Object, MockPets.Object, MockAnimals.Object,
                                               MockInteractions.Object, Config, responseBuilder.UserSessionBuilder,
                                               MockContainer.Object));
        }
Exemplo n.º 10
0
        public async Task <IHttpActionResult> AddTool(dynamic data)
        {
            data = Newtonsoft.Json.JsonConvert.DeserializeObject <ExpandoObject>(Convert.ToString(data), new ExpandoObjectConverter());
            int     userID   = UserSessionState.UserID(data.token);
            Code    status   = default(Code);
            dynamic response = null;
            var     dbObj    = new Tool()
            {
                Name         = data.name,
                CategoryID   = Convert.ToInt32(data.category),
                Cell         = Convert.ToInt32(data.cell),
                Count        = Convert.ToInt32(data.count),
                IsDeleted    = false,
                CreationDate = DateTimeOffset.UtcNow.DateTime
            };

            if (ExpandoHelper.PropertyExists(data, "low"))
            {
                dbObj.LowCount = Convert.ToInt32(data.low);
            }
            if (ExpandoHelper.PropertyExists(data, "lower"))
            {
                dbObj.LowerCount = Convert.ToInt32(data.lower);
            }
            store.Tools.Add(dbObj);
            store.Audits.Add(new Audit()
            {
                Action       = (int)ToolAction.Add,
                Count        = dbObj.Count,
                Readed       = false,
                ToolID       = dbObj.ToolID,
                WorkerID     = store.Users.FirstOrDefault(u => u.WorkerID == userID).WorkerID,
                CreationDate = DateTimeOffset.UtcNow.DateTime,
            });
            store.SaveChanges();
            response = new
            {
                id         = dbObj.ToolID,
                name       = dbObj.Name,
                cell       = dbObj.Cell,
                low        = dbObj.LowCount,
                lower      = dbObj.LowerCount,
                toolscount = dbObj.Count,
                toolsinuse = 0,
            };
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }
Exemplo n.º 11
0
 public override void OnActionExecuting(HttpActionContext actionContext)
 {
     if (actionContext.ActionArguments.ContainsKey(this.Parameter) &&
         actionContext.Request.Method == HttpMethod.Get)
     {
         var filter = actionContext.ActionDescriptor.GetCustomAttributes <HttpGetTypeAttribute>(true).FirstOrDefault();
         if (filter != null)
         {
             var qs = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);
             if (qs.HasKeys())
             {
                 var data   = JsonConvert.DeserializeObject(qs[this.Parameter], filter.RequestObjectType);
                 var status = Validator.Validate(data);
                 try
                 {
                     if (status != default(Code))
                     {
                         throw new Exception();
                     }
                     if (data is TokenBase)
                     {
                         if (!UserSessionState.TokenIsValid((data as TokenBase).token))
                         {
                             status = Code.AuthRequired;
                             throw new Exception();
                         }
                     }
                     else
                     {
                         status = Code.BadRequest;
                         throw new Exception();
                     }
                     actionContext.ActionArguments[Parameter] = data;
                 }
                 catch
                 {
                     actionContext.Response = actionContext.Request.CreateResponse(
                         HttpStatusCode.OK,
                         ApiResponseManager.CreateResponse(new Status(status)));
                 }
             }
         }
     }
     base.OnActionExecuting(actionContext);
 }
Exemplo n.º 12
0
        public bool UserSessionDisconnect(string connectionName, string connectionId, int userSessionId)
        {
            EnsureValidClientConnection(connectionName, connectionId);

            UserSessionState userSessionState = new UserSessionState(userSessionId);
            UserSession      userSession      = new UserSession(UserSessionType.Disconnected);
            UserInfo         userInfo         = new UserInfo();

            if (UserSessionHelper.GetUserSessionUserInfo(userSessionId, out userInfo))
            {
                userSession.UserLoginInfo = userInfo;
            }
            else
            {
                // For machines without auto logon, password retrieval fails. We would like to
                // throw an exception here to fail the test case.
                throw new InvalidOperationException("Auto logon is required to be enabled for this task to succeed");
            }

            return(StateManager.Current.Transition(connectionName, userSessionState, userSession, UserSessionAction.Disconnect));
        }
Exemplo n.º 13
0
        public async Task <IHttpActionResult> GetUserTypes([FromBody] dynamic data)
        {
            Code    status   = default(Code);
            dynamic response = null;

            int?userID = UserSessionState.UserID(data.token);

            if (userID.HasValue)
            {
                response = store
                           .UserTypes
                           .Select(ut => new
                {
                    id   = ut.UserTypeID,
                    name = ut.Name
                });
            }
            else
            {
                status = Code.AuthRequired;
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }
Exemplo n.º 14
0
        public async Task <HttpResponseMessage> ValidateUser([FromBody] LoginUserModel user)
        {
            var    status = default(Code);
            string token  = null;
            var    dbuser = store
                            .Users
                            .FirstOrDefault(u => u.UserName.ToLower().CompareTo(user.username.ToLower()) == 0);
            dynamic response = null;

            if (dbuser != null)
            {
                if (dbuser.IsDeleted == null || (dbuser.IsDeleted != null && !dbuser.IsDeleted.Value))
                {
                    if (dbuser.Password.ToUpper().CompareTo(PasswordGenerator.GetPasswordSHA1(Convert.ToString(user.password), dbuser.Salt)) == 0)
                    {
                        token               = UserSessionState.AddNew(dbuser.WorkerID, user.rememberme);
                        response            = new ExpandoObject();
                        response.rememberme = user.rememberme;
                        response.username   = dbuser.UserName;
                        response.privilege  = dbuser.UserType.AccessLevel;
                    }
                    else
                    {
                        status = Code.LoginFailed;
                    }
                }
                else
                {
                    status = Code.UserHasDeleted;
                }
            }
            else
            {
                status = Code.LoginFailed;
            }
            return(Request.CreateResponse(HttpStatusCode.OK, ApiResponseManager.CreateResponse(new Status(status), (object)response, (!string.IsNullOrWhiteSpace(token) ? token : null))));
        }
Exemplo n.º 15
0
        public async Task <IHttpActionResult> DeleteTool([FromBody] SimpleDeleteModel data)
        {
            Code status   = default(Code);
            int? userId   = UserSessionState.UserID(Convert.ToString(data.token));
            int? response = null;

            if (userId != null)
            {
                var c = ToolsHelper.ToolStatByToolID(store, data.id);
                if (c <= 0)
                {
                    var tool = store
                               .Tools
                               .FirstOrDefault(t => t.ToolID == data.id);
                    tool.IsDeleted = true;
                    store
                    .DeletedTools
                    .Add(new DeletedTool()
                    {
                        ToolID      = data.id,
                        WorkerID    = store.Users.FirstOrDefault(u => u.WorkerID == userId.Value).WorkerID,
                        DeletedDate = DateTimeOffset.UtcNow.DateTime
                    });
                    store.SaveChanges();
                    response = data.id;
                }
                else
                {
                    status = Code.ToolIsInUse;
                }
            }
            else
            {
                status = Code.AuthRequired;
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }
Exemplo n.º 16
0
        public async Task <IHttpActionResult> ChangePassword([FromBody] ChangePasswordModel data)
        {
            var status   = default(Code);
            var response = false;
            int?userID   = UserSessionState.UserID(data.token);

            if (userID.HasValue)
            {
                var dbuser = store
                             .Users
                             .Where(u => u.WorkerID.CompareTo(userID.Value) == 0 && (!u.IsDeleted.HasValue || (u.IsDeleted.HasValue && !u.IsDeleted.Value)))
                             .FirstOrDefault();
                if (dbuser != null)
                {
                    if (dbuser.Password.ToUpper().CompareTo(PasswordGenerator.GetPasswordSHA1(Convert.ToString(data.oldpass), dbuser.Salt)) != 0)
                    {
                        status = Code.OldPassIncorrect;
                    }
                    else
                    {
                        dbuser.Password = PasswordGenerator.GetPasswordSHA1(data.newpass, dbuser.Salt);
                        store.SaveChanges();
                        response = true;
                    }
                }
                else
                {
                    status = Code.NotFound;
                }
            }
            else
            {
                status = Code.AuthRequired;
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }
Exemplo n.º 17
0
 public static void SetUserSessionState(this HttpContext httpContext, UserSessionState sessionState)
 {
     httpContext.Session.Set <UserSessionState>("user", sessionState);
 }
Exemplo n.º 18
0
        public async Task <IHttpActionResult> WriteOffTool([FromBody] WriteOffModel data)
        {
            Code    status   = default(Code);
            dynamic response = null;

            if (data.count > 0)
            {
                int?userID = UserSessionState.UserID(data.token);
                if (userID.HasValue)
                {
                    var allTools = store
                                   .Tools
                                   .Where(t => (!t.IsDeleted.HasValue || (t.IsDeleted.HasValue && !t.IsDeleted.Value)) &&
                                          !t.Category.IsDeleted &&
                                          t.ToolID == data.id)
                                   .Select(t => t.Count)
                                   .ToList();
                    var all        = allTools.Count > 0 ? allTools.Sum() : 0;
                    int inuseCount = Math.Abs(ToolsHelper.ToolStatByToolID(store, data.id));
                    if (data.count <= all - inuseCount)
                    {
                        store
                        .WriteOffTools
                        .Add(new WriteOffTool()
                        {
                            ToolID       = data.id,
                            WorkerID     = store.Users.FirstOrDefault(u => u.WorkerID == userID.Value).WorkerID,
                            Count        = data.count,
                            Comment      = data.comment,
                            WriteOffTime = DateTimeOffset.UtcNow.DateTime
                        });
                        store.Audits.Add(new Audit()
                        {
                            Action       = (int)ToolAction.WriteOff,
                            Count        = data.count,
                            Readed       = false,
                            ToolID       = data.id,
                            WorkerID     = store.Users.FirstOrDefault(u => u.WorkerID == userID.Value).WorkerID,
                            CreationDate = DateTimeOffset.UtcNow.DateTime,
                        });
                        var tool = store
                                   .Tools
                                   .FirstOrDefault(t => t.ToolID == data.id);
                        if (tool != null)
                        {
                            response = tool.Count -= data.count;
                            store.SaveChanges();
                        }
                        else
                        {
                            status = Code.NotFound;
                        }
                    }
                    else
                    {
                        status = Code.OperationFailed;
                    }
                }
                else
                {
                    status = Code.AuthRequired;
                }
            }
            return(Ok(ApiResponseManager.CreateResponse(new Status(status), response)));
        }