예제 #1
0
        public HttpResponseMessage Login(string username, string password)
        {
            HttpResponseMessage response;

            using (SecureCloud_Entities entity = new SecureCloud_Entities())
            {
                int count = entity.T_DIM_USER.Count(u => u.USER_NAME == username && u.USER_PWD == password && u.USER_IS_ENABLED);
                if (count > 0)
                {
                    Guid guid = Guid.NewGuid();
                    response = this.Request.CreateResponse(
                        HttpStatusCode.OK,
                        new JObject(
                            new JProperty("authorized", true),
                            new JProperty("token", guid.ToString())));
                    int?roleId         = null;
                    int userId         = -1;
                    var firstOrDefault = entity.T_DIM_USER.FirstOrDefault(u => u.USER_NAME == username);
                    if (firstOrDefault != null)
                    {
                        roleId = firstOrDefault.ROLE_ID;
                        userId = firstOrDefault.USER_NO;
                    }

                    string deviceToken = this.Request.GetQueryString("deviceToken");
                    var    authInfo    = new AuthorizationInfo
                    {
                        UserId      = userId,
                        RoleId      = roleId,
                        DeviceToken = deviceToken,
                        Token       = guid.ToString(),
                        HashCode    =
                            this.auth.GetHashValue(
                                guid.ToString(),
                                Request.GetClientIp()),
                        AuthorisedResources = new List <string>()
                    };

                    var authorizationResources = from s in entity.T_DIM_ROLE_RESOURCE where s.ROLE_ID == roleId select s.RESOURCE_ID.Trim();

                    authInfo.AuthorisedResources.AddRange(authorizationResources);

                    this.auth.RemoveVerifyTicket(guid.ToString());
                    this.auth.SaveVerifyTicket(guid.ToString(), authInfo);
                    this.Request.Properties["AuthorizationInfo"] = authInfo;

                    // 更新设备令牌
                    if (deviceToken != null)
                    {
                        var item = entity.T_DIM_DEVICETOKEN.Where(d => d.DeviceToken == deviceToken);
                        foreach (var i in item)
                        {
                            var entry = entity.Entry(i);
                            entry.State = System.Data.EntityState.Deleted;
                        }

                        var record = new T_DIM_DEVICETOKEN {
                            DeviceToken = deviceToken, OnlineUser = username
                        };
                        var entry2 = entity.Entry(record);
                        entry2.State = System.Data.EntityState.Added;

                        entity.SaveChanges();
                    }
                }
                else
                {
                    response = this.Request.CreateResponse(
                        HttpStatusCode.OK,
                        new JObject(
                            new JProperty("authorized", false),
                            new JProperty("token", string.Empty)));
                }
            }

            return(response);
        }
예제 #2
0
        public HttpResponseMessage LoginAndReturnInfo(string username, string password)
        {
            HttpResponseMessage response;

            using (SecureCloud_Entities entity = new SecureCloud_Entities())
            {
                var ur = (from r in entity.T_DIM_ROLE
                          from u in entity.T_DIM_USER
                          where u.ROLE_ID == r.ROLE_ID &&
                          u.USER_NAME == username &&
                          u.USER_PWD == password &&
                          u.USER_IS_ENABLED
                          select u.ROLE_ID);
                List <UserLogin> users = new List <UserLogin>();
                if (!ur.ToList().Any())
                {
                    response = this.Request.CreateResponse(
                        HttpStatusCode.OK,
                        new JObject(
                            new JProperty("authorized", false),
                            new JProperty("token", string.Empty),
                            new JProperty("userId", string.Empty),
                            new JProperty("email", string.Empty),
                            new JProperty("orgId", string.Empty),
                            new JProperty("organization", string.Empty),
                            new JProperty("roleId", string.Empty),
                            new JProperty("roleCode", string.Empty),
                            new JProperty("systemName", string.Empty),
                            new JProperty("logo", string.Empty)));
                }
                else
                {
                    var uos = (from u in entity.T_DIM_USER
                               from oo in entity.T_DIM_ORGANIZATION
                               where oo.ID == u.USER_ORG &&
                               u.USER_NAME == username &&
                               u.USER_PWD == password &&
                               u.USER_IS_ENABLED
                               select oo.SystemName);
                    if (ur.FirstOrDefault() == 1)
                    {
                        var query =
                            from u in entity.T_DIM_USER
                            from r in entity.T_DIM_ROLE
                            from o in entity.T_DIM_ORGANIZATION
                            where u.ROLE_ID == r.ROLE_ID &&
                            u.USER_NAME == username &&
                            u.USER_PWD == password &&
                            u.USER_IS_ENABLED
                            select new UserLogin
                        {
                            USER_NO     = u.USER_NO,
                            USER_EMAIL  = u.USER_EMAIL,
                            orgid       = o.ID,
                            ABB_NAME_CN = o.ABB_NAME_CN,
                            ROLE_ID     = r.ROLE_ID,
                            ROLE_CODE   = r.ROLE_CODE,
                            SystemName  = uos.FirstOrDefault() == null ? o.SystemName : uos.FirstOrDefault(),
                            Logo        = o.Logo
                        };
                        users = query.ToList();
                    }
                    else
                    {
                        var query =
                            from u in entity.T_DIM_USER
                            from r in entity.T_DIM_ROLE
                            join uo in entity.T_DIM_USER_ORG
                            on u.USER_NO equals uo.USER_NO into org
                            from or in org.DefaultIfEmpty()
                            join o in entity.T_DIM_ORGANIZATION
                            on or.ORGANIZATION_ID equals o.ID
                            into uor
                            from uo in uor.DefaultIfEmpty()
                            where u.ROLE_ID == r.ROLE_ID &&
                            u.USER_NAME == username &&
                            u.USER_PWD == password &&
                            u.USER_IS_ENABLED
                            select new UserLogin
                        {
                            USER_NO     = u.USER_NO,
                            USER_EMAIL  = u.USER_EMAIL,
                            orgid       = uo.ID,
                            ABB_NAME_CN = uo.ABB_NAME_CN,
                            ROLE_ID     = r.ROLE_ID,
                            ROLE_CODE   = r.ROLE_CODE,
                            SystemName  = uos.FirstOrDefault() == null ? uo.SystemName : uos.FirstOrDefault(),
                            Logo        = uo.Logo
                        };
                        users = query.ToList();
                    }
                    //var users = query.ToList();
                    if (users.Any())
                    {
                        Guid guid = Guid.NewGuid();
                        var  info = users.First();
                        response = this.Request.CreateResponse(
                            HttpStatusCode.OK,
                            new JObject(
                                new JProperty("authorized", true),
                                new JProperty("token", guid.ToString()),
                                new JProperty("userId", info.USER_NO),
                                new JProperty("email", info.USER_EMAIL),
                                new JProperty("orgId", info.orgid),
                                new JProperty("organization", info.ABB_NAME_CN),
                                new JProperty("roleId", info.ROLE_ID),
                                new JProperty("roleCode", info.ROLE_CODE),
                                new JProperty("systemName", info.SystemName),
                                new JProperty("logo", info.Logo)));

                        this.auth.RemoveVerifyTicket(guid.ToString());
                        string deviceToken = this.Request.GetQueryString("deviceToken");
                        var    authInfo    = new AuthorizationInfo
                        {
                            UserId      = (int)info.USER_NO,
                            RoleId      = info.ROLE_ID,
                            DeviceToken = deviceToken,
                            Token       = guid.ToString(),
                            HashCode    =
                                this.auth.GetHashValue(
                                    guid.ToString(),
                                    Request.GetClientIp()),
                            AuthorisedResources = new List <string>()
                        };

                        var authorizationResources = from s in entity.T_DIM_ROLE_RESOURCE where s.ROLE_ID == info.ROLE_ID select s.RESOURCE_ID.Trim();

                        authInfo.AuthorisedResources.AddRange(authorizationResources);

                        this.auth.SaveVerifyTicket(guid.ToString(), authInfo);
                        this.Request.Properties["AuthorizationInfo"] = authInfo;

                        // 更新移动令牌
                        if (deviceToken != null)
                        {
                            var item = entity.T_DIM_DEVICETOKEN.Where(d => d.DeviceToken == deviceToken);
                            foreach (var i in item)
                            {
                                var entry = entity.Entry(i);
                                entry.State = System.Data.EntityState.Deleted;
                            }

                            var record = new T_DIM_DEVICETOKEN {
                                DeviceToken = deviceToken, OnlineUser = username
                            };
                            var entry2 = entity.Entry(record);
                            entry2.State = System.Data.EntityState.Added;

                            entity.SaveChanges();
                        }
                    }
                    else
                    {
                        response = this.Request.CreateResponse(
                            HttpStatusCode.OK,
                            new JObject(
                                new JProperty("authorized", false),
                                new JProperty("token", string.Empty),
                                new JProperty("userId", string.Empty),
                                new JProperty("email", string.Empty),
                                new JProperty("orgId", string.Empty),
                                new JProperty("organization", string.Empty),
                                new JProperty("roleId", string.Empty),
                                new JProperty("roleCode", string.Empty),
                                new JProperty("systemName", string.Empty),
                                new JProperty("logo", string.Empty)));
                    }
                }
            }

            return(response);
        }