Author Topic: Apply Security Lock  (Read 4161 times)

zalbina

  • EA User
  • **
  • Posts: 149
  • Karma: +0/-0
    • View Profile
Apply Security Lock
« on: September 04, 2012, 11:14:45 pm »
Hello,

I need to lock some diagrams in a package using VBA code like Apply Lock in EA Project browser. The part of the code:

currentDiagram.ApplyUserLock

does not work properly. I have the message "Method ApplyUserLock of the object IDualDiagram failed.

Some ideas?

Thanks.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Apply Security Lock
« Reply #1 on: September 05, 2012, 12:59:46 am »
Try GetLastError() to find out the cause.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Apply Security Lock
« Reply #2 on: September 05, 2012, 04:43:21 pm »
Locking elements via the API is a bit tricky.
You have to know whether or not you or another user already has an element locked before trying.
After some fiddling around I came up with the following solution:

To get the name of the user who is currently locking the element
Code: [Select]
/// <summary>
/// returns the name of the user currently locking this element
/// </summary>
/// <returns>the name of the user currently locking this element</returns>
public override string getLockedUser()
{
      string lockedUser = string.Empty;
      //if (this.wrappedElement.Locked)
      //{
            string SQLQuery = @"select u.FirstName, u.Surname from t_seclocks s
                                          inner join t_secuser u on s.userID = u.userID
                                          where s.entityID = '" + this.wrappedElement.ElementGUID + "'";
            XmlDocument result = ((EAModel)this.model).SQLQuery(SQLQuery);
            XmlNode firstNameNode = result.SelectSingleNode("//FirstName");
            XmlNode lastNameNode = result.SelectSingleNode("//Surname");
            if (firstNameNode != null && lastNameNode != null)
            {
                  lockedUser = firstNameNode.InnerText + " " + lastNameNode.InnerText;
            }
      //}
      return lockedUser;
}
To know whether or not an element is locked
Code: [Select]
/// <summary>
/// returns true if currently locked
/// </summary>
/// <returns>true if currently locked</returns>
public override bool isLocked()
{
      return (this.getLockedUser() != string.Empty);
}
To know whether an element is read-only (= not locked by me)
Code: [Select]
/// <summary>
/// returns true if this element is readonly
/// </summary>
/// <returns>true if this element is readonly</returns>
public override bool isReadOnly()
{
      if (this.isLocked())
      {
            string SQLQuery = @"select s.UserID from t_seclocks s
                                          where s.entityID = '" + this.wrappedElement.ElementGUID + "'";
            XmlDocument result = ((EAModel)this.model).SQLQuery(SQLQuery);
            XmlNode userIDNode = result.SelectSingleNode("//UserID");
            if (userIDNode.InnerText.Equals(((EAModel)this.model).getWrappedModel().GetCurrentLoginUser(true), StringComparison.InvariantCultureIgnoreCase))
                  return false;
      }
      //else
      return true;
}
To lock an element:
Code: [Select]
/// <summary>
/// locks the element by the current user
/// </summary>
/// <returns>true if successful</returns>
public override bool enableWrite()
{
      //because of some nasty bug in EA the application will crash if the element is already locked
      // by another user.
      // therefore we check first if this element is locked before even trying.
      string SQLQuery = "select s.entityID from t_seclocks s where s.entityID = '" + this.wrappedElement.ElementGUID + "'";
      XmlDocument result = ((EAModel)this.model).SQLQuery(SQLQuery);
      XmlNode lockNode = result.SelectSingleNode("//entityID");
      if (lockNode == null)
      {
            //no lock found, go ahead and try to lock the element
            try
            {
                  return this.wrappedElement.ApplyUserLock();
            }
            catch (Exception)
            {
                  return false;
            }
      }
      else  
      {
            //lock found, don't even try it.
            return false;
      }
}
To unlock it:
Code: [Select]
/// <summary>
/// unlocks this element
/// </summary>
public override void unLock()
{
      //EA doesn't want to release user locks from other users, so we do it directly in the database
      string SQLDeleteLock = @"delete from t_seclocks where EntityID = '" + this.getGUID() + "'";
      ((EAModel)this.model).getWrappedModel().Execute(SQLDeleteLock);

      //this.wrappedElement.ReleaseUserLock();
}

The code for Diagrams or Packages is basically a variation on the same theme (although you might want to implement a lock on all elements owned by the package, or all elements shown on the diagram)

Geert

zalbina

  • EA User
  • **
  • Posts: 149
  • Karma: +0/-0
    • View Profile
Re: Apply Security Lock
« Reply #3 on: September 05, 2012, 11:29:24 pm »
Thanks a lot. It's very clear.

zalbina

  • EA User
  • **
  • Posts: 149
  • Karma: +0/-0
    • View Profile
Re: Apply Security Lock
« Reply #4 on: September 10, 2012, 04:40:42 pm »
Hello again,

I'm very confused now. My code suddenly stoped to work properly. This part of code always return true and it does not matter if element is locked or not:

If currentElement.Locked = True Then
    collectionElement.Add currentElement.name
Else
    currentElement.ApplyUserLock
End If

What could be wrong with it? Thanks.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Apply Security Lock
« Reply #5 on: September 11, 2012, 05:29:43 pm »
Have you changed something to the security settings in EA?

Geert

zalbina

  • EA User
  • **
  • Posts: 149
  • Karma: +0/-0
    • View Profile
Re: Apply Security Lock
« Reply #6 on: September 11, 2012, 11:38:42 pm »
No, but I re-write the code and check "If Locked" using sql query and it works by this strange way.

Thank you very much.